+ All Categories
Home > Documents > Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C...

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

Date post: 04-Jan-2016
Category:
Upload: lee-glenn
View: 230 times
Download: 1 times
Share this document with a friend
21
Lab 5 Sockets
Transcript
Page 1: Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions .

Lab 5 Sockets

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

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

Sockets Programming http://www.scit.wlv.ac.uk/~jphb/comms/sockets.htmlSocket Interface

http://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

Beej's Guide to Network Programming http://www.ecst.csuchico.edu/~beej/guide/netSockets Tutorial http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html

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

Compiling Socket Code#include <netdb.h>

#include <sys/socket.h>

gcc -lsocket -lnsl -g -Wall -o foo foo.c

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

Outgoing Socket#include <netdb.h>#include <sys/socket.h>#define HTTP_PORT 80int 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 StructuresOne will get filled in for us..We'll have to copy info into the other...

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

gethostbynamestruct 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

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

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?

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

PortsEach IP address has 65,536 "Ports" associated

with it. Some of these ports have standardized uses:

Port Use21 FTP Server (Control)22 SSH Server80 HTTP Server443 Secure HTTP (TLS/SSL)

We have to select the port number that we want to access on the server:

serverAddr.sin_port = htons(HTTP_PORT);

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

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);

}

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

Connecting Outgoing Socketint server_response;

server_response = connect(our_socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

if(server_response < 0) { printf("cannot connect\n"); exit(1);

}

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

Connect

int connect(int sockfd,

const struct sockaddr *serv_addr,

socklen_t addrlen);

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

Reading from Socketchar temp;int bytes_read;while (1) {

bytes_read = recv(our_socket, &temp, 1, 0);

if (bytes_read != 1) { break;}printf (“%c\n”, temp);

}

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

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_sizeYou can set additional flags or use 0 for flags

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

Sending Dataint 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;

}

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

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_sizeYou can set additional flags or use 0 for flags

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

Closing the Socketshutdown(our_socket,SHUT_RDWR);

SHUT_RD = No more receptions;

SHUT_WR = No more transmissions;

SHUT_RDWR = No more receptions or transmissions.

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

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);

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

Creating Incoming Socket

our_socket = socket(AF_INET, SOCK_STREAM, 0);

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

Binding Incoming Addressint response;

response = bind(our_socket, (struct sockaddr *) &localAddr, sizeof(localAddr));

if(response < 0)

{ exit(1);

}

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

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 connectionrequests will be queued before further requests arerefused.Returns 0 on success, -1 for errors

extern int listen (int socket, int N);

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

Accept Incoming Socketint nsock;

nsock = accept(our_socket, 0, 0);

int accept (int listening_socket, struct sockaddr * address,socklen_t * address_length);

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

FTP ServerComputer10.0.1.100

Lab Computer10.0.1.X(x is a number)

Port 21 Control Connection

Data Connection

Commands

Your File


Recommended