Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions

Similar presentations


Presentation on theme: "Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions"— Presentation transcript:

1 Lab 5 Sockets

2 Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions http://www.softlab.ntua.gr/facilities/documentation/unix /unix-socket-faq/unix-socket-faq.html http://www.softlab.ntua.gr/facilities/documentation/unix /unix-socket-faq/unix-socket-faq.html Sockets Programming http://www.scit.wlv.ac.uk/~jphb/comms/sockets.html Socket Interface http://www.netbook.cs.purdue.edu/cs363/lecture_notes/ chap22/chap22_0.htmlhttp://www.netbook.cs.purdue.edu/cs363/lecture_notes/ chap22/chap22_0.html BSD Sockets: A Quick and Dirty Primer http://world.std.com/~jimf/papers/sockets/sockets.html http://world.std.com/~jimf/papers/sockets/sockets.html Beej's Guide to Network Programming http://www.ecst.csuchico.edu/~beej/guide/net Sockets Tutorial http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html

3 Compiling Socket Code #include gcc -lsocket -lnsl -g -Wall -o foo foo.c

4 Outgoing Socket #include #define HTTP_PORT 80 int main() { int our_socket; struct sockaddr_in serverAddr; struct hostent *destination; destination = gethostbyname("ftp.redhat.com"); //Also works with “10.0.1.100” format if(destination==NULL) { printf("No such host\n"); exit(1); } Two Data Structures One will get filled in for us.. We'll have to copy info in to the other...

5 gethostbyname struct hostent *gethostbyname(const char *name); People operate the Internet with names.. i.e. "www.google.com", "ftp.redhat.com" The Internet itself operates using 32 bit numbers to identify computers. These numbers are known as Internet Protocol (IP) addresses. gethostbyname() finds, builds and returns address of a data structure with information on the web site/ftp site with the specified name

6 Outgoing Socket (cont'd) //now we have to copy info from //one data structure to another //That's all these 2 lines do... serverAddr.sin_family = destination->h_addrtype; memcpy( (char *)&serverAddr.sin_addr.s_addr, destination->h_addr_list[0], destination->h_length ); What does the memcpy function do? What are its parameters?

7 Ports Each IP address has 65,536 "Ports" associated with it. Some of these ports have standardized uses: PortUse 21FTP Server (Control) 22SSH Server 80HTTP Server 443Secure HTTP (TLS/SSL) We have to select the port number that we want to access on the server: serverAddr.sin_port = htons(HTTP_PORT);

8 Outgoing Socket serverAddr.sin_port = htons(HTTP_PORT); //htons = 'host to network' //remember big and little endian? our_socket = socket(AF_INET, SOCK_STREAM, 0); if(our_socket < 0) { printf("cannot open socket\n"); exit(1); }

9 Connecting Outgoing Socket int server_response; server_response = connect(our_socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)); if(server_response < 0) { printf("cannot connect\n"); exit(1); }

10 Connect int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);

11 Reading from Socket char temp; int bytes_read; while (1) { bytes_read = recv(our_socket, &temp, 1, 0); if (bytes_read != 1) { break; } printf (“%c\n”, temp); }

12 Receiving Data Returns -1 if unsuccessful. Otherwise number of bytes read. Must pass in pointer to a buffer that you want to read in to and the size of that buffer. size_t recv (int socket, void *buffer, size_t buffer_size, int flags) You can cast an int to a size_t for buffer_size You can set additional flags or use 0 for flags

13 Sending Data int bytes_sent; char request[18]= “GET / HTTP/1.0\012\015\012\015”; bytes_sent = send(our_socket, request, strlen(request), 0); if (bytes_sent != strlen(request)) { printf("We didn't send the command properly\n"); return 0; }

14 Sending Data Returns -1 if unsuccessful. Otherwise number of bytes sent. Must pass in pointer to a buffer that you want to send and the size of that buffer. size_t send (int socket, void *buffer, size_t buffer_size, int flags) You can cast an int to a size_t for buffer_size You can set additional flags or use 0 for flags

15 Closing the Socket shutdown(our_socket,SHUT_RDWR); SHUT_RD = No more receptions; SHUT_WR = No more transmissions; SHUT_RDWR = No more receptions or transmissions.

16 Incoming Socket #define DATA_PORT 5999 int our_socket; struct sockaddr_in localAddr; /* Set up data socket */ localAddr.sin_family = AF_INET; localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(DATA_PORT);

17 Creating Incoming Socket our_socket = socket(AF_INET, SOCK_STREAM, 0);

18 Binding Incoming Address int response; response = bind(our_socket, (struct sockaddr *) &localAddr, sizeof(localAddr)); if(response < 0) { exit(1); }

19 Listening for Incoming int response, N=1; response = listen(our_socket, N); if (response != 0) { printf("Cannot listen on port\n"); } Prepare to accept connections on socket. N connection requests will be queued before further requests are refused. Returns 0 on success, -1 for errors extern int listen (int socket, int N);

20 Accept Incoming Socket int nsock; nsock = accept(our_socket, 0, 0); int accept (int listening_socket, struct sockaddr * address, socklen_t * address_length);

21 FTP Server Computer 10.0.1.100 Lab Computer 10.0.1.X (x is a number) Port 21 Control Connection Data Connection Commands Your File


Download ppt "Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions"

Similar presentations


Ads by Google