Socket Programming

17
Socket Programming Lab 1 1 CS 332 - Computer Networks

description

Socket Programming. Lab 1. Berkeley Unix IPC. socket --an interprocess communication endpoint supporting operations similar to those supported by files opened with the open system call. Creating a socket. int s = socket (domain, type, protocol); - PowerPoint PPT Presentation

Transcript of Socket Programming

Page 1: Socket Programming

Socket Programming

Lab 1

1CS 332 - Computer Networks

Page 2: Socket Programming

Berkeley Unix IPCProcess A

Process B

= Socket CommunicationChannel

socket--an interprocess communication endpoint supporting operations similar to those supported by files opened with the open system call.

2CS 332 - Computer Networks

Page 3: Socket Programming

Creating a socketint s = socket (domain, type, protocol);

• domain - the “communication domain” to be used by the socket. One of:

• AF_INET—The Internet Protocol v4.• AF_INET6 – The Internet Protocol v6.

• type -what kind of service the process wants on the socket. Choices are:

• SOCK_STREAM--two way byte stream• SOCK_DGRAM--communication consists of individual

messages.

• protocol --what protocol is to be used for communication over the socket. 0 asks for the default protocol for this protocol family for the requested socket type.

3CS 332 - Computer Networks

Page 4: Socket Programming

Specifying Addresses

• General socket addresses:struct sockaddr {

unsigned short sa_family;char sa_data[14];

};

• sa_family is the address family, such as AF_INET, for the Internet address family

• sa_data is 14 bytes of family specific address data.

4CS 332 - Computer Networks

Page 5: Socket Programming

Specifying addresses

• There is a special address struct for internet addresses:struct sockaddr_in {

short sin_family;

u_short sin_port;

struct in_addr sin_addr;

char sin_zero[8];

};

5CS 332 - Computer Networks

Page 6: Socket Programming

Specifying addresses

• Note: same size as sockaddr

• sin_family – the address family to use, always AF_INET for us

• sin_port – the port number for this address

• sin_addr – the 32 bit Internet address

6CS 332 - Computer Networks

Page 7: Socket Programming

Connecting Sockets

• In order for some other process to connect to us, it needs to know what machine we are on and also how to specify the process that the socket it is trying to connect to.

• We must request a “network visible process address" for our socket.

• This address is called a port number, and we get it by doing a bind operation.

• Port numbers < 1024 are reserved for system use.

• Necessary only for processes acting as a server.

7CS 332 - Computer Networks

Page 8: Socket Programming

Connecting Sockets

status = bind (sock, name, namelen);• sock - a socket descriptor returned by a

previous call to the socket system call.• name - a structure describing the port we want

to bind to. bind() expects a structure of type sockaddr, but for the AF_INET domain, we use a sockaddr_in structure, as defined in /usr/include/netinet/in.h

• namelen -- size of the structure passed in name.

8CS 332 - Computer Networks

Page 9: Socket Programming

Waiting for connections

• Once a socket is created and bound to a port, a server will typically wait for other processes to connect to it using the listen() system call.

listen(sock, n);• This notifies the system that we are willing to

accept up to n connection requests on sock. If multiple requests are made before we handle them, they are queued.

• Listen does not block; it merely notifies the OS that we are ready to begin accepting connection requests.

9CS 332 - Computer Networks

Page 10: Socket Programming

Accepting connections

• In order to wait for connections, we use the accept() system call.

new_sock = accept (sock, addr, addrlen);• sock--the socket that we did the listen call on• addr– pointer to information about the connection that is

created. Can be NULL if you aren’t interested.• addrlen—pointer to length in bytes of the structure

passed in addr. Contains actual len. of struct containing client address info returned in addr.

• new_sock--socket created by the accept. The original socket is still available for new connect attempts.

10CS 332 - Computer Networks

Page 11: Socket Programming

Client connection requests

• There is a bit less work involved on the client side. socket() is used to create a socket, then connect() is used to connect to an existing socket at the other end:

status = connect (client_sock, name, namelen);• client_sock--a socket created by a call to socket().• name--structure describing the host and port to connect

to. Of type sockaddr_in. The in_addr field can be filled in by using gethostbyname().

• namelen--the length of the structure passed to name.

11CS 332 - Computer Networks

Page 12: Socket Programming

Host address information

host_info = gethostbyname(hostname);

• hostname--a character string containing the name of the host to connect to.

• host_info--a structure of type hostent (defined in /usr/include/netdb.h). The h_addr field can by copied to the sin_addr field of the sockaddr_in structure passed to connect().

12CS 332 - Computer Networks

Page 13: Socket Programming

Sending/Receiving with sockets

bytesSent = send( sock, buf, size, flags );

• sock--socket created by an accept or socket/connect.

• buf--buffer to hold the incoming bytes. type void * (generic pointer)

• size--maximum number of bytes in buf.• flags – can be used to change default

behavior of send. 0 says use defaults• bytesSent – number of bytes actually

sent.13CS 332 - Computer Networks

Page 14: Socket Programming

Sending/Receiving with sockets

nbytes = recv (sock, buf, size, flags);• sock--socket created by an accept() or socket()/connect() sequence.

• buf--buffer for received bytes. type void * (generic pointer)

• size—max number of bytes to put in buf.• flags – can be used to change default

behavior of send. 0 says use defaults• nbytes--number of bytes actually received.

14CS 332 - Computer Networks

Page 15: Socket Programming

Sending/Receiving with sockets

• Sockets created with type SOCK_STREAM behave just like files opened with the open() system call. The read() system call can be used to receive bytes, the write() system call can be used to send bytes.

nbytes = read(new_sock, buf, size);• new_sock--socket created by an accept or

socket/connect.• buf--buffer to hold the incoming bytes.• size--maximum number of bytes to put into buf.• nbytes--number of bytes actually read.

15CS 332 - Computer Networks

Page 16: Socket Programming

Sending/Receiving with sockets

nbytes = write (new_sock, buf, size);• new_sock--socket created by an accept() or socket()/connect() sequence.

• buf--buffer containing string to write.• size--number of bytes contained in buf.• nbytes--number of bytes actually written.

16CS 332 - Computer Networks

Page 17: Socket Programming

Server Network Client

Port

Liste

n S

ocke

t

Com

munic

atio

n s

ocke

t

Com

munic

atio

n s

ocke

t

lsock=socket();

bind (lsock, port)‘

listen(lsock)

scomm=accept

read(scomm, buf)

write(scomm, buf)

ccomm=socket()

connect (ccom name)

write (ccomm, buf)

read (ccomm, buf)

close (scomm) close(ccomm)

17CS 332 - Computer Networks