1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| 服务器 # include <sys/socket.h> # include <arpa/inet.h> # include <netdb.h> # include <stdio.h> # include <stdlib.h> # include <string.h> # include <unistd.h>
# define SERVERPORT 8888 # define LOCALHOST "127.0.0.1" # define MYMSGLEN 2048
//功能函数,用来判断是否是回文 int palindrom ( char * s ) { char * t = s + strlen ( s ) - 1 ; while ( * s == * t ) s ++, t -- ; return s >= t ; }
//在无限循环中实现客户端数据的接受recv,处理palindrom和返回结果send int answer_client ( int sock ) { // Receive the length of the string. int code, length ; char string [ MYMSGLEN ]; while ( 1 ) { // Receive the string of characters. printf ( "Waiting for a string to process....\n" ) ; memset ( string, 0, sizeof( string ) ); length = recv ( sock, string, sizeof ( string ), 0 ) ; if ( length == -1 ) { perror ( "recv" ) ; code = close ( sock ) ; if ( code == -1 ) perror ( "close" ) ; return ( -1 ) ; }
if ( length == 0 ) { // Connection closed by remote peer, release ressources. printf ( "Connection closed by remote peer ....\n" ) ; code = close ( sock ) ; if ( code == -1 ) { perror ( "close" ) ; return ( -1 ) ; } return ( 0 ) ; } printf ( "Going to process the received string '%s'\n", string ) ; // Check the string of characters. int ret ; ret = palindrom ( string ) ;
// Return the answer. printf ( "Processing done, sending back the answer\n" ) ; code = send ( sock, & ret, sizeof ( int ), 0 ) ; if ( code == -1 ) { perror ( "send" ) ; close ( sock ) ; return ( -1 ) ; } } }
//socket创建+设置+bind地址 int naming ( ) { // Socket creation. int sock ; //AF_INET:IPv4 protocols。定义在<sys/socket.h>中, //SOCK_STREAM: connected communication withend to end control flow sock = socket ( AF_INET, SOCK_STREAM, 0 ) ; if ( sock == -1 ) { perror ( "socket" ) ; exit ( 1 ) ; }
// Reuse the same port (useful when developing...). int code, enable = 1; //SO_REUSEADDR:Allows other sockets to bind() to this port code = setsockopt ( sock, SOL_SOCKET, SO_REUSEADDR, & enable, sizeof ( int ) ) ; if (code == -1 ) { perror ( "setsockopt" ) ; close ( sock ) ; exit ( 1 ) ; }
code = setsockopt ( sock, SOL_SOCKET, SO_REUSEPORT, & enable, sizeof ( int ) ) ; if (code == -1 ) { perror ( "setsockopt" ) ; close ( sock ) ; exit ( 1 ) ; } // Initialisation of the sockaddr_in data structure. struct sockaddr_in addr ; memset ( & addr, 0, sizeof ( struct sockaddr_in ) ) ; addr . sin_family = AF_INET ; addr . sin_port = htons(SERVERPORT) ; addr . sin_addr . s_addr = inet_addr(LOCALHOST) ;
// Name the socket. code = bind ( sock, ( struct sockaddr * ) & addr, sizeof ( struct sockaddr_in ) ) ; if ( code == -1 ) { perror ( "bind" ) ; close ( sock ) ; exit ( 1 ) ; }
// Return the socket ID. return sock ; }
int main ( int argc, char * argv [ ] ) { // Name the socket. int sock ; sock = naming ( ) ;
// Set up listening on the port. int code ; //backlog为0时在linux上表明允许不受限制的连接数,这是一个缺陷,因为它可能会导致SYN Flooding(拒绝服务型攻击) code = listen ( sock, 0 ) ; if ( code == -1 ) { perror ( "listen" ) ; close ( sock ) ; exit ( 1 ) ; }
// Wait for incoming connections and deal with the clients. int new ; socklen_t len ; struct sockaddr_in addr ; struct sockaddr_in newAddr ; struct sockaddr_in peerAddr ; for ( ; ; ) { printf ( "Server waiting for a new connection \n" ) ; len = sizeof ( struct sockaddr_in ) ; //获取到用来与客户端通信的socket new = accept ( sock, ( struct sockaddr * ) & addr, & len ) ; if ( new == -1 ) { perror ( "accept" ) ; close ( sock ) ; exit ( 1 ) ; } printf ( " A new connection from --> %s:%d \n" ,inet_ntoa ( addr.sin_addr ), ntohs ( addr.sin_port ) ) ; len = sizeof ( struct sockaddr_in ) ; code = getsockname ( new, ( struct sockaddr * ) & newAddr, & len ) ; if ( code == -1 ) { perror ( "getsockname" ) ; close ( sock ) ; close ( new ) ; exit ( 1 ) ; } len = sizeof ( struct sockaddr_in ) ; code = getpeername ( new, ( struct sockaddr * ) & peerAddr, & len ) ; if ( code == -1 ) { perror ( "getpeername" ) ; close ( sock ) ; close ( new ) ; exit ( 1 ) ; } printf ( " The local address bound to the new socket --> %s:%d \n" , inet_ntoa ( newAddr.sin_addr ), ntohs ( newAddr.sin_port ) ) ; printf ( " The peer address bound to the new socket --> %s:%d \n" , inet_ntoa ( peerAddr.sin_addr ), ntohs ( peerAddr.sin_port ) ) ; //以上打印连接信息 //以下调用answer_client()与client通信 if ( answer_client ( new ) == ( -1 ) ) { close ( sock ) ; exit ( 0 ) ; } }
// This point in the program will never be reached. return ( 0 ) ; }
|