Header Files

#define _Windows #ifndef _Windows // Header files for UNIX/LINUX #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> // #else // Header files for Windows #include <winsock.h> #include <memory.h> #define bzero(x, y) memset(x, 0, y) #define bcopy(x, y, z) memcpy(y, x, z) // #endif #include <stdio.h>

Huai-Jen Liu

Startup and Cleanup Example
WSADATA Kernel_Data; if(WSAStartup(0x0002, &Kernel_Data)) { // "Winsock DLL not found! " … } … WSACleanup();

Huai-Jen Liu

“…”.Demo 1: TCP client “…” (argv[3]) server Hello. How are you? Huai-Jen Liu .

TCP Network Applications client socket() initialize sockaddr_in structure server socket() initialize sockaddr_in structure bind() listen() accept() send() / recv() closesocket() Huai-Jen Liu connect() recv() / send() closesocket() closesocket() .

addrlen. struct sockaddr_in serverAddr.Client 1 (1/9) main(argc. msgLen. char *argv[]. struct hostent *serverhp. char in_buf[BUFLEN]. int clientSock. Huai-Jen Liu . sockname. argv) int argc.

BUFLEN)) { printf("CLIENT: local hostname is %s\n". } Huai-Jen Liu .Client 1 (2/9) if(argc < 4) { puts("Usage: client remotehostname port name"). in_buf). } /* get local hostname */ if(!gethostname(in_buf. exit(1).

").Client 1 (3/9) if((clientSock=socket(AF_INET. IPPROTO_TCP)) < 0) { puts("CLIENT: socket() error. exit(1). SOCK_STREAM. } Huai-Jen Liu .

&serverAddr. serverhp->h_length).Client 1 (4/9) bzero((char *)&serverAddr.").sin_addr. if((serverhp=gethostbyname(argv[1])) == NULL) { puts("CLIENT: gethostbyname() error. exit(1). serverAddr. close(clientSock). Huai-Jen Liu . sizeof(struct sockaddr_in)). serverAddr.sin_family=AF_INET.sin_port=htons(atoi(argv[2])). } bcopy(serverhp->h_addr.

(struct sockaddr *)&serverAddr. close(clientSock).Client 1 (5/9) if(connect(clientSock. } Huai-Jen Liu ."). sizeof(struct sockaddr_in))) { puts("CLIENT: connect() error. exit(1).

printf("CLIENT: local sockname.Client 1 (6/9) addrlen=sizeof(struct sockaddr).sin_addr=%s\n"."). if(getsockname(clientSock. close(clientSock). ntohs(sockname. inet_ntoa(sockname. exit(1). (struct sockaddr *)&sockname. &addrlen)) { puts("CLIENT: getsockname() error.sin_port=%d\n". } printf("CLIENT: local sockname. Huai-Jen Liu .sin_port)).sin_addr)).

inet_ntoa(sockname. Huai-Jen Liu .sin_port=%d\n".sin_addr)). } printf("CLIENT: peer sockname.sin_port)). exit(1).Client 1 (7/9) addrlen=sizeof(struct sockaddr)."). (struct sockaddr *)&sockname. printf("CLIENT: peer sockname.sin_addr=%s\n". ntohs(sockname. close(clientSock). if(getpeername(clientSock. &addrlen)) { puts("CLIENT: getpeername() error.

Huai-Jen Liu .").Client 1 (8/9) puts("CLIENT: Ready to send."). } puts("CLIENT: Sending Complete. 1). if(send(clientSock. strlen(argv[3])."). 0) < 0) { puts("CLIENT: send() error. shutdown(clientSock. exit(1). argv[3]. close(clientSock).

while((msgLen=recv(clientSock. Huai-Jen Liu close(clientSock). BUFLEN). in_buf). close(clientSock). bzero(in_buf. Client 1 (9/9) if(msgLen < 0) { puts("CLIENT: recv() error. } puts("CLIENT: Close the socket."). printf("%s". 0)) > 0) { in_buf[msgLen]='\0'. in_buf."). BUFLEN. .bzero(in_buf. exit(1). } putchar('\n'). BUFLEN).

clientAddr. clientSock. msgLen. exit(1). clientLen. serverLen. out_buf[BUFLEN].Server 1 (1/9) int serverSock."). addrLen. struct sockaddr_in serverAddr. } Huai-Jen Liu . IPPROTO_TCP)) < 0) { puts("SERVER: socket() error. SOCK_STREAM. char in_buf[BUFLEN]. if((serverSock=socket(AF_INET. sockAddr.

sin_addr.").sin_family=AF_INET. if(bind(serverSock. } Huai-Jen Liu . serverAddr. sizeof(struct sockaddr_in)). sizeof(struct sockaddr_in))) { puts("SERVER: bind() error. close(serverSock). serverAddr.s_addr=htonl(INADDR_ANY). (struct sockaddr *)&serverAddr.Server 1 (2/9) bzero((char *)&serverAddr. serverAddr.sin_port=0. exit(1).

} printf("SERVER: Local Port Number is %d\n". exit(1). ntohs(serverAddr. Huai-Jen Liu .Server 1 (3/9) serverLen=sizeof(struct sockaddr_in). inet_ntoa(serverAddr.sin_addr)). printf("SERVER: Local IP Address is %s\n". close(serverSock). &serverLen)) { puts("SERVER: getsockname() error.sin_port)). (struct sockaddr *)&serverAddr."). if(getsockname(serverSock.

close(serverSock). 1)) { puts("SERVER: listen() error.Server 1 (4/9) if(listen(serverSock. exit(1)."). } Huai-Jen Liu .

printf("SERVER: Client IP Address is %s\n".sin_addr)). ntohs(clientAddr. inet_ntoa(clientAddr. if((clientSock=accept(serverSock. } printf("SERVER: Client Port Number is %d\n". Huai-Jen Liu ."). exit(1). &clientLen)) < 0) { puts("SERVER: accept() error. (struct sockaddr *)&clientAddr. close(serverSock).Server 1 (5/9) clientLen=sizeof(struct sockaddr_in).sin_port)).

"). ntohs(sockAddr. if(getpeername(clientSock. (struct sockaddr *)&sockAddr.Server 1 (6/9) addrLen=sizeof(struct sockaddr_in). close(serverSock). exit(1).sin_addr)). inet_ntoa(sockAddr. &addrLen)) { puts("SERVER: getpeername() error.sin_port)). } printf("SERVER: Peer Port Number is %d\n". printf("SERVER: Peer IP Address is %s\n". Huai-Jen Liu .

0)) < 0) { puts("SERVER: recv() error. } in_buf[msgLen]='\0'. in_buf. ntohs(clientAddr. if((msgLen=recv(clientSock. close(serverSock). BUFLEN).bzero(in_buf. msgLen).sin_addr). Huai-Jen Liu . BUFLEN. printf("SERVER: Message from %s with port %d\n".sin_port))."). in_buf. inet_ntoa(clientAddr. 0). close(clientSock). Server 1 (7/9) shutdown(clientSock. printf("SERVER: Client Name is '%s' of length %d\n". exit(1).

} bcopy(in_buf. Huai-Jen Liu } Server 1 (8/9) "Hello. 0) < 0) { puts("SERVER: send(s1) error. s1. " . exit(1). strlen(s1). exit(1). BUFLEN). close(serverSock). if(send(clientSock."). out_buf.puts("SERVER: Ready to send. msgLen.'."). out_buf[msgLen++]='\n'. close(clientSock). close(clientSock). close(serverSock)."). out_buf. out_buf[msgLen++]='. if(send(clientSock. 0) < 0) { puts("SERVER: send() error.

close(clientSock). close(clientSock). close(serverSock). } puts("SERVER: Sending Complete. Huai-Jen Liu "How are you?" . 0) < 0) { puts("SERVER: send(s2) error. s2.Server 1 (9/9) if(send(clientSock."). strlen(s2)."). exit(1). close(serverSock).

168.tw CLIENT: local sockname.0. $ ./a.254 SERVER: Message from 192.out SERVER: Local Port Number is 2748 SERVER: Local IP Address is 0.0. CLIENT: Sending Complete.10 CLIENT: Ready to send.0.sin_port=2462 CLIENT: local sockname.254 with port 2462 SERVER: Client Name is 'Liu' of length 3 SERVER: Ready to send. Huai-Jen Liu $ .0.edu.168.out SERVER: Sending Complete.0 SERVER: Client Port Number is 2462 SERVER: Client IP Address is 192. Usage: client remotehostname port name $ $ .254 CLIENT: peer sockname.10 2748 Liu CLIENT: local hostname is livingstone.0./a.out 192.0.0.chu.168.sin_addr=192.$. Hello. Liu.168. How are you? CLIENT: Close the socket.sin_addr=192.254 SERVER: Peer Port Number is 2462 SERVER: Peer IP Address is 192./a.sin_port=2748 CLIENT: peer sockname.168.0.168.

Sign up to vote on this title
UsefulNot useful