?? udpserver.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 iSockfd; int iRet = -1; int iServerPort; struct sockaddr_in server_addr; struct sockaddr_in client_addr; char * cLogin = "login :"; char * cPasswd = "passwd :"; char sLogin[50]; char sPasswd[50]; char * pLogin = "nxb"; char * pPasswd = "1234"; 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] ); } iServerPort = atoi ( argv[1] ); snprintf ( pLogName , sizeof ( pLogName ) , "%s" , argv[2] ); iSockfd = socket ( AF_INET , SOCK_DGRAM , 0 ); if ( iSockfd == -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 = htonl ( INADDR_ANY ); bzero ( & ( server_addr.sin_zero ) , 8 ); if ( bind ( iSockfd , ( struct sockaddr * )&server_addr , sizeof ( struct sockaddr ) ) == -1 ) { printf ( "Bind Error !\n" ); exit ( 1 ); } printf ( "Bind Success !\n" ); while ( 1 ) { iSin_size = sizeof ( struct sockaddr ); memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) ); iRet = recvfrom ( iSockfd , sReceiveBuff , RECEIVE_BUFF_SIZE , 0 , ( struct sockaddr * ) &client_addr , &iSin_size ); if ( iRet == -1 ) { printf ( "Receive Error !\n" ); }else if ( iRet == 0 ) { printf ( "Client Closed !\n" ); close ( iSockfd ); break; }else { printf ( "Receive Message : %s\n" , sReceiveBuff ); memset ( sPrintLogBuff , 0 , sizeof ( sPrintLogBuff ) ); snprintf ( sPrintLogBuff , sizeof ( sPrintLogBuff ) , "Receive Message : %s\n" , sReceiveBuff ); iWriteLog ( pLogName , sPrintLogBuff ); } printf ( "Send Message : " ); scanf ( "%s" , &sKeyBoardInput ); memset ( sPrintLogBuff , 0 , sizeof ( sPrintLogBuff ) ); snprintf ( sPrintLogBuff , sizeof ( sPrintLogBuff ) , "Send Message : %s\n" , sKeyBoardInput ); iWriteLog ( pLogName , sPrintLogBuff ); iRet = sendto ( iSockfd , sKeyBoardInput , strlen( sKeyBoardInput ) , 0 , ( struct sockaddr * ) &client_addr , iSin_size ); if ( iRet == -1 ) { printf ( "Send Error !\n" ); } }}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 + -