Lab 5 Sockets
Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions /unix-socket-faq/unix-socket-faq.html /unix-socket-faq/unix-socket-faq.html Sockets Programming Socket Interface chap22/chap22_0.htmlhttp:// chap22/chap22_0.html BSD Sockets: A Quick and Dirty Primer Beej's Guide to Network Programming Sockets Tutorial
Compiling Socket Code #include gcc -lsocket -lnsl -g -Wall -o foo foo.c
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 “ ” 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...
gethostbyname struct hostent *gethostbyname(const char *name); People operate the Internet with names.. i.e. " "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
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?
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);
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); }
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); }
Connect int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);
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); }
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
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; }
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
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.
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);
Creating Incoming Socket our_socket = socket(AF_INET, SOCK_STREAM, 0);
Binding Incoming Address int response; response = bind(our_socket, (struct sockaddr *) &localAddr, sizeof(localAddr)); if(response < 0) { exit(1); }
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);
Accept Incoming Socket int nsock; nsock = accept(our_socket, 0, 0); int accept (int listening_socket, struct sockaddr * address, socklen_t * address_length);
FTP Server Computer Lab Computer X (x is a number) Port 21 Control Connection Data Connection Commands Your File