Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.

Similar presentations


Presentation on theme: "TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication."— Presentation transcript:

1 TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication –Release connection –Reliable (Acknowledged, in order) UDP: –No need to setup a link –Independent packets –Not reliable (no acknowledgement)

2 Programming with Sockets Sockets are Berkeley software distribution UNIX interface to network protocols Endpoint of communication, has a type and one associated process Uses file system model (open-close-read-write) Internet vs. Unix domain

3 Socket Creation s = socket( domain, type, protocol ) AF_INET AF_UNIX SOCK_STREAM SOCK_DGRAM #include ( 0 ) SOCKET DESCRIPTOR ( int type )

4 Connection Establishment connect(socket, server address, size of address structure) s = socket pointer returned from socket command Server information, the data structure that keeps this information is : struct sockaddr_in { short int sin_family;//address type(AF_INET) struct in_addr sin_addr;//internet address of server int sin_port;//port number of server }; struct sockaddr_in server; Size of the data structure (struct sockaddr_in)

5 How to get server address? You may either give the server name (or address) from the command line or paste it directly into the code. There are 2 ways to get the server’s internet address: If the name of the server is given, you can use struct hostent * = gethostbyname(char * name) hostent data type has two fields you will use: struct in_addr h_addr : internet address of server int h_length : length of the address The last step will be to copy the internet address into our server’s address field. Ex. struct hostent *hp; hp = gethostbyname(“sun114-16.cise.ufl.edu”); memcpy( (char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length);

6 How to get server address? The second way is to convert the Internet host address from the standard numbers-and-dots notation into binary data. (ex. 128.56.560.67, this is not the correct ip address of our server, please use your knowledge to find out the ip address of our server, the server’s host name is sun114-16.cise.ufl.edu) The function we will use is: int inet_aton( const char * name, struct in_addr * addr); This function converts the Internet host address name from the standard numbers-and-dots notation into binary data and stores it in the struct in_addr that addr points to. inet_aton returns nonzero if the address is valid, zero if not. Ex. Inet_aton(“128.56.560.67”, &server.sin_addr);

7 Putting it all together int s; struct sockaddr_in server; struct hostent * hp; s = socket( AF_INET, SOCK_STREAM, 0 ); server.sin_family = AF_INET; hp = gethostbyname( argv[1] ); memcpy( (char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length); server.sin_port = htons( atoi(argv[2]) ); connect( s, (struct sockaddr *) &server, sizeof(server) );

8 Data Transfer int nbytes = write( int socket, char * data, int length ); It is similar to writing to a file. The first parameter is a pointer to socket, the second parameter is the data you wish to send, and the last parameter is the size of data. Reading from the socket is done using: int nbytes = read( int socket, char * data, int max_length ); nbytes gives you the number of bytes read from the socket. The difference from write is that since we do not know the size of the stream to be received in advanced, we set the third parameter to the maximum length of stream that our data buffer may hold. In case of error both read and write commands return -1.

9 Data Transfer Example #define MAXMSG 256 char buffer[MAXMSG] ; int nbytes; buffer = “this is a test.”; nbytes = write(s, buffer, strlen(buffer)+1); nbytes = read(s, buffer, MAXMSG);

10 Closing Connection close( int socket ) SOCKET DESCRIPTOR A SOCK_STREAM socket can be discarded by a close() system call. Ex. close( s );

11 Client-Server Architecture request response Process request client Host: sun114-16.cise.ufl.edu Port: 7000

12 The Algorithm Set the client id Connect to the server Send the command to the server Get the input Receive the response and print it Close the connection input = “logout” input = “login”

13 Additional Notes You will use a data format in your application to send to our server. The data format is a data structure with 3 fields. Please be sure to fill all the fields correctly. After you create the PDU, you have to cast it to char pointer in the write command. Ex. struct PDU message; // message is your data structure. You have to set // message.username, message.type, and if necessary // message.payload fields. //now you are ready to send it nbytes = write( s, (char *)&message, sizeof(message) ); What you will read from our server is a string (char *). So, you do not have to do casting. Once you read it to a buffer, just print the string to the standard output. You can refer to read example in slide 9.

14 Additional Notes We recommend you to use standard C in a UNIX environment. To compile your program you should use the following line: gcc client.c –lsocket –lnsl –o client


Download ppt "TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication."

Similar presentations


Ads by Google