1 / 14

TCP/IP Protocol Stack

TCP/IP Protocol Stack. Application. Sockets. (Gate to network). TCP. UDP. IP. Device Drivers. Programming with Sockets. Sockets are Berkeley software distribution UNIX interface to network protocols Endpoint of communication, has a type and one associated process

tayten
Download Presentation

TCP/IP Protocol Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TCP/IP Protocol Stack Application Sockets (Gate to network) TCP UDP IP Device Drivers

  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 ( 0 ) s = socket( domain , type , protocol ) SOCKET DESCRIPTOR ( int type ) AF_INET SOCK_STREAM AF_UNIX SOCK_DGRAM #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <sys/types.h>

  4. Connection Establishment connect(socket, server address, size of address structure) s = socket pointer returned from socket command Size of the data structure (struct sockaddr_in) • 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;

  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 thataddr 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 Process request client response Host: sun114-16.cise.ufl.edu Port: 7000

  12. Set the client id Connect to the server input = “logout” Receive the response and print it Get the input Send the command to the server Close the connection The Algorithm 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

More Related