?? udpclient.c
字號:
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/stat.h>#include <sys/time.h>#include <assert.h>#include <fcntl.h>#include <netinet/in.h>#include <netdb.h>#include <assert.h>#include <sys/errno.h>#include <errno.h>#include <strings.h>#include <string.h>#include <netinet/in.h>#include <arpa/inet.h>#include <signal.h>#include <unistd.h>#define BACKLOG 20#define RECEIVE_BUFF_SIZE 1024*5int main( int argc , char ** argv ) { int iSin_size; int iServerfd; int iRet; char sServerIp[20]; int iServerPort; struct hostent * host; struct sockaddr_in server_addr; char * pLogin = "login :"; char * pPasswd = "passwd :"; char sLogin[50]; char sPasswd[50]; int iLogin = 1; char sReceiveBuff[RECEIVE_BUFF_SIZE]; char sKeyBoardInput[RECEIVE_BUFF_SIZE]; char sPrintLogBuff[RECEIVE_BUFF_SIZE]; char pLogName[100]; if ( argc < 2 ) { printf ( "Usage : %s parameter\n" , argv[0] ); } if ( ( host = gethostbyname ( argv[1] ) ) == NULL ) { printf ( "Gethostbyname Error !\n" ); exit ( 1 ); } snprintf ( sServerIp , sizeof ( sServerIp ) , "%s" , argv[1] ); iServerPort = atoi ( argv[2] ); snprintf ( pLogName ,sizeof ( pLogName ) , "%s" , argv[3] ); iServerfd = socket ( AF_INET , SOCK_DGRAM , 0 ); if ( iServerfd == -1 ) { printf ( "Socket Found error !\n" ); exit ( 1 ); } printf ( "Socket Found Success !\n" ); server_addr.sin_family = AF_INET; server_addr.sin_port = htons ( iServerPort ); server_addr.sin_addr.s_addr = inet_addr ( sServerIp ); //server_addr.sin_addr.s_addr = htonl (* ( unsigned int * ) * host->h_addr_list ); bzero ( & ( server_addr.sin_zero ) , 8 );while ( 1 ) { iSin_size = sizeof ( server_addr ); if ( connect ( iServerfd , ( struct sockaddr * ) &server_addr , iSin_size ) == -1 ) { printf ( "Connect Error !\n" ); sleep ( 1 ); continue; } printf ( "Connect Ip : %s\nPort : %d\n" , sServerIp , iServerPort ); while ( 1 ) { memset ( sPrintLogBuff , 0 , sizeof ( sPrintLogBuff ) ); while ( 1 ) { printf ( "Send Message : " ); scanf ( "%s" , &sKeyBoardInput ); snprintf ( sPrintLogBuff , sizeof ( sPrintLogBuff ) , "Send Message : %s\n" , sKeyBoardInput ); iWriteLog ( pLogName , sPrintLogBuff ); iRet = send ( iServerfd , sKeyBoardInput , strlen( sKeyBoardInput ) , 0 ); if ( iRet == -1 ) { printf ( "Send Error !\n" ); } memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) ); iRet = recv ( iServerfd , sReceiveBuff , RECEIVE_BUFF_SIZE , 0 ); if ( iRet == -1 ) { printf ( "Receive Error !\n" ); }else if ( iRet == 0 ) { printf ( "Server Closed !\n" ); close ( iServerfd ); exit ( 1 ); }else { printf ( "Receive Message : %s\n" , sReceiveBuff ); memset ( sPrintLogBuff , 0 , sizeof ( sPrintLogBuff ) ); snprintf ( sPrintLogBuff , sizeof ( sPrintLogBuff ) , "Receive Message : %s\n" , sReceiveBuff ); iWriteLog ( pLogName , sPrintLogBuff ); } } }}}int iWriteLog (char * pFileName, char * pLogBuff){ int iOpenfd; int iWriteNum; iOpenfd = open ( pFileName , O_APPEND|O_RDWR|O_CREAT , S_IRWXU ); iWriteNum = write (iOpenfd , pLogBuff , strlen ( pLogBuff ) ); return iWriteNum;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -