Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network Programming Week #1 J.P. Yoo

Similar presentations


Presentation on theme: "Network Programming Week #1 J.P. Yoo"— Presentation transcript:

1 Network Programming Week #1 J.P. Yoo willow@konkuk.ac.kr

2 TCP/IP Sockets in C: Practical Guide for Programmers Michael J. Donahoo Kenneth L. Calvert

3 Network API Interface between application and protocol software. Application Network API Protocol A Protocol B Protocol C

4 Socket Programming Interface between application and network Network API sets for TCP/IP protocol suite A socket is an abstract representation of a communication endpoint. Sockets work with Unix I/O services just like files, pipes & FIFOs. –Difference establishing a connection specifying communication endpoint addresses

5 Unix Descriptor Table Descriptor Table 0 1 2 3 4 Data structure for file 0 Data structure for file 1 Data structure for file 2

6 Socket Descriptor Structure Descriptor Table 0 1 2 3 4 Family: PF_INET Service: SOCK_STREAM Local IP: 111.22.3.4 Remote IP: 123.45.6.78 Local Port: 2249 Remote Port: 3726 Family: PF_INET Service: SOCK_STREAM Local IP: 111.22.3.4 Remote IP: 123.45.6.78 Local Port: 2249 Remote Port: 3726

7 Sockets Identified by protocol and local/remote address/port Applications may refer to many sockets Sockets accessed by many applications

8 Creating a Socket create a resource for communication endpoint –socket call does not specify where data will be coming from, nor where it will be going to – it just creates the interface! Socket reference –File (socket) descriptor in UNIX –Socket handle in WinSock FamilyType Protocol TCP PF_INET SOCK_STREAMIPPROTO_TCP UDPSOCK_DGRAMIPPROTO_UDP int socket(int family,int type,int proto);

9 Two essential types of sockets SOCK_STREAM –a.k.a. TCP –reliable delivery –in-order guaranteed –connection-oriented –bidirectional SOCK_DGRAM –a.k.a. UDP –unreliable delivery –no order guarantees –no notion of connection – app indicates dest. for each packet –can send or receive

10 Data I/O int main() { int fd; fd = open( "data.dat", O_RDONLY,0); while( 1 ) { write( fd, test,sizeof(test)+1 ); } close( out ); return 0; } int main() { int sockfd; sd = socket( PF_INET, SOCK_STREAM,0); while( 1 ) { write( sd, test,sizeof(test)+1 ); } close( out ); return 0; } To where?

11 Specifying an Endpoint Addr. Sockets API is generic. There must be a generic way to specify endpoint addresses. TCP/IP requires an IP address and a port number for each endpoint address –We will deal with only TCP/IP suite –Other protocol suites (families) may use other schemes

12 TCP/IP Addresses We don t need to deal with sockaddr structures since we will only deal with a real protocol family(TCP/IP). We can use sockaddr_in structures.

13 struct sockaddr { unsigned short sa_family;/* Address family (e.g., AF_INET) */ char sa_data[14]; /* Protocol-specific address information */ }; struct sockaddr_in { unsigned short sin_family;/* Internet protocol (AF_INET) */ unsigned short sin_port; /* Port (16-bits) */ struct in_addr sin_addr; /* Internet address (32-bits) */ char sin_zero[8]; /* Not used */ }; struct in_addr { unsigned long s_addr; /* Internet address (32-bits) */ }; Generic IP Specific sockaddr sockaddr_in Family Port Blob Internet address Not used 2 bytes 4 bytes 8 bytes

14 Assigning an addr. to a socket The bind() system call is used to assign an address to an existing socket. bind returns 0 if successful or -1 on error. int bind( int sockfd, struct sockaddr *localaddr, int addrlen);

15 bind() Example int mysock,err; struct sockaddr_in myaddr; char* servIP; /* ex)203.252.164.143 */ mysock = socket(PF_INET,SOCK_STREAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( portnum ); myaddr.sin_addr.s_addr = inet_addr(servIP); err=bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr));

16 Socket Flow Server Socket() Bind() Client Socket() Listen() Accept() Recv() Send() Connect() Send() Recv() Block until connect Process request Connection Establishmt. Data (request) Data (reply)

17 listen() Used by connection-oriented servers to indicate an application is willing to receive connections int listen(int socket, int queuelimit) –Socket: handle of newly creates socket Only 1 for server –queuelimit: number of connection requests that can be queued by the system while waiting for server to execute accept call.

18 Accept() int accept(int socket, struct sockaddr *clientdaddress, int addr_len) After executing listen, the accept() carries out a passive open –Create socket for the client which is doing connect() it returns with a new socket that corresponds with new connection and the address contains the clients address

19 Connect() int connect(int socket, struct sockaddr *address, int addr_len) Client executes an active open of a connection Address field contains remote system s address Client OS usually selects random, unused port

20 After connection has been made, application uses send/recv to data int send(int socket, char *message, int msg_len, int flags) –Send specified message using specified socket int recv(int scoket, char *buffer, int buf_len, int flags) –Receive message from specified socket into specified buffer Send(to), Recv(from)

21 Socket Flow(revisit) Server Socket() Bind() Client Socket() Listen() Accept() Recv() Send() Connect() Send() Recv() Block until connect Process request Connection Establishmt. Data (request) Data (reply)

22 Client: Initiates the connection Server: Passively waits to respond Clients and Servers Client: Bob Hi. Im Bob. Nice to meet you, Jane. Server: Jane Hi, Bob. Im Jane

23 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Assign a port to socket 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection Server starts by getting ready to receive client connections…

24 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection /* Create socket for incoming connections */ if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed");

25 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);/* Any incoming interface */ echoServAddr.sin_port = htons(echoServPort); /* Local port */ if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("bind() failed");

26 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection /* Mark the socket so it will listen for incoming connections */ if (listen(servSock, MAXPENDING) < 0) DieWithError("listen() failed");

27 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection for (;;) /* Run forever */ { clntLen = sizeof(echoClntAddr); if ((clntSock=accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen)) < 0) DieWithError("accept() failed");

28 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection Server is now blocked waiting for connection from a client Later, a client decides to talk to the server…

29 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection /* Create a reliable, stream socket using TCP */ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed");

30 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */ echoServAddr.sin_port = htons(echoServPort); /* Server port */ if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("connect() failed");

31 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection if ((clntSock=accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen)) < 0) DieWithError("accept() failed");

32 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection echoStringLen = strlen(echoString); /* Determine input length */ /* Send the string to the server */ if (send(sock, echoString, echoStringLen, 0) != echoStringLen) DieWithError("send() sent a different number of bytes than expected");

33 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection /* Receive message from client */ if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0) DieWithError("recv() failed");

34 TCP Client/Server Interaction Client 1.Create a TCP socket 2.Establish connection 3.Communicate 4.Close the connection Server 1.Create a TCP socket 2.Bind socket to a port 3.Set socket to listen 4.Repeatedly: a.Accept new connection b.Communicate c.Close the connection close(sock); close(clntSocket)

35 TCPEchoClient.c(1/2) #include /* for printf() and fprintf() */ #include /* for socket(), connect(), send(), and recv() */ #include /* for sockaddr_in and inet_addr() */ #include /* for atoi() and exit() */ #include /* for memset() */ #include /* for close() */ #define RCVBUFSIZE 32 /* Size of receive buffer */ void DieWithError(char *errorMessage); /* Error handling function */ int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in echoServAddr; /* Echo server address */ unsigned short echoServPort; /* Echo server port */ char *servIP; /* Server IP address (dotted quad) */ char *echoString; /* String to send to echo server */ char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */ unsigned int echoStringLen; /* Length of string to echo */ int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv() and total bytes read */ if ((argc 4)) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s [ ]\n", argv[0]); exit(1); } servIP = argv[1]; /* First arg: server IP address (dotted quad) */ echoString = argv[2]; /* Second arg: string to echo */ if (argc == 4) echoServPort = atoi(argv[3]); /* Use given port, if any */ else echoServPort = 7; /* 7 is the well-known port for the echo service */ /* Create a reliable, stream socket using TCP */ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); /* Construct the server address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */ echoServAddr.sin_port = htons(echoServPort); /* Server port */

36 TCPEchoClient.c(2/2) /* Establish the connection to the echo server */ if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("connect() failed"); echoStringLen = strlen(echoString); /* Determine input length */ /* Send the string to the server */ if (send(sock, echoString, echoStringLen, 0) != echoStringLen) DieWithError("send() sent a different number of bytes than expected"); /* Receive the same string back from the server */ totalBytesRcvd = 0; printf("Received: "); /* Setup to print the echoed string */ while (totalBytesRcvd < echoStringLen) { /* Receive up to the buffer size (minus 1 to leave space for a null terminator) bytes from the sender */ if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0) DieWithError("recv() failed or connection closed prematurely"); totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */ echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */ printf(echoBuffer); /* Print the echo buffer */ } printf("\n"); /* Print a final linefeed */ close(sock); exit(0);

37 TCPEchoServer.c(1/2) #include /* for printf() and fprintf() */ #include /* for socket(), bind(), and connect() */ #include /* for sockaddr_in and inet_ntoa() */ #include /* for atoi() and exit() */ #include /* for memset() */ #include /* for close() */ #define MAXPENDING 5 /* Maximum outstanding connection requests */ void DieWithError(char *errorMessage); /* Error handling function */ void HandleTCPClient(int clntSocket); /* TCP client handling function */ int main(int argc, char *argv[]) { int servSock; /* Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ struct sockaddr_in echoServAddr; /* Local address */ struct sockaddr_in echoClntAddr; /* Client address */ unsigned short echoServPort; /* Server port */ unsigned int clntLen; /* Length of client address data structure */ if (argc != 2) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } echoServPort = atoi(argv[1]); /* First arg: local port */ /* Create socket for incoming connections */ if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); /* Construct local address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ echoServAddr.sin_port = htons(echoServPort); /* Local port */ /* Bind to the local address */ if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("bind() failed"); /* Mark the socket so it will listen for incoming connections */ if (listen(servSock, MAXPENDING) < 0) DieWithError("listen() failed");

38 TCPEchoServer.c(2/2) for (;;) /* Run forever */ { /* Set the size of the in-out parameter */ clntLen = sizeof(echoClntAddr); /* Wait for a client to connect */ if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0) DieWithError("accept() failed"); /* clntSock is connected to a client! */ printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); HandleTCPClient(clntSock); } /* NOT REACHED */ }

39 HandleTCPClient.c #include /* for printf() and fprintf() */ #include /* for recv() and send() */ #include /* for close() */ #define RCVBUFSIZE 32 /* Size of receive buffer */ void DieWithError(char *errorMessage); /* Error handling function */ void HandleTCPClient(int clntSocket) { char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */ int recvMsgSize; /* Size of received message */ /* Receive message from client */ if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0) DieWithError("recv() failed"); /* Send received string and receive again until end of transmission */ while (recvMsgSize > 0) /* zero indicates end of transmission */ { /* Echo message back to client */ if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize) DieWithError("send() failed"); /* See if there is more data to receive */ if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0) DieWithError("recv() failed"); } close(clntSocket); /* Close client socket */ }

40 Winsock #include #include #include void main() { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) { fprintf(stderr, "WSAStartup() failed"); exit(1); } … closesocket(sock); }

41 Winsock for visual studio, you must add wsock32.lib manually –menu->project->settings->link->wsock32.lib */

42 Assignment #0 client Server –Linux windows cygwin. www.cygwin.com – – gcc –Client Part TcpEchoClient.c, DieWithError.c –Server Part TcpEchoServer.c, DieWithError.c, HandleTcpClient.c

43 Gcc #gcc –o client TcpEchoClient.c DieWithError.c Output binary filenameSource files

44 Assignment #1 –.. echo_history.log append. Challenge –, permission.


Download ppt "Network Programming Week #1 J.P. Yoo"

Similar presentations


Ads by Google