Socket Programming Tutorial: TCP Client and Server, Study notes of Computer Science

An introduction to socket programming with a focus on tcp clients and servers. It covers creating sockets, binding and connecting to a server, sending and receiving data, and handling errors. It also includes code snippets and references to stevens' unix network programming for further reading.

Typology: Study notes

Pre 2010

Uploaded on 03/28/2010

koofers-user-q0y-1
koofers-user-q0y-1 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Socket Programming
Part II
Code snipet:
struct sockaddr_in myAddressStruct;
//Fill in the address information into myAddressStruct here, (will be explained in detail shortly)
connect(socket_file_descriptor, (struct sockaddr *) &myAddressStruct, sizeof(myAddressStruct));
Now lets discuss how to fill in the sockaddr_in structure:
struct sockaddr_in{
sa_family_t sin_family /*Address/Protocol Family*/
unit16_t sin_port /* Port number --Network Byte Order-- */
struct in_addr sin_addr /*A struct for the 32 bit IP Address */
unsigned char sin_zero[8] /*Just ignore this it is just padding*/
};
struct in_addr{
unit32_t s_addr /*32 bit IP Address --Network Byte Order-- */
};
For the sa_family variable sin_family always use the constant: PF_INET or AF_INET
***Always initialize address structures with bzero() or memset() before filling them in ***
***Make sure you use the byte ordering functions when necessary for the port and IP
address variables otherwise there will be strange things a happening to your packets***
Converting between dotted decimal strings and Network Address values (Read pgs 70 – 74 Stevens)
To convert a string dotted decimal IP4 address to a NETWORK BYTE ORDERED 32 bit value use the
functions:
inet_addr()
inet_aton()
To convert a 32 bit NETWORK BYTE ORDERED to a IP4 dotted decimal string use:
inet_ntoa()
Read Stevens also for more recent functions which work with both IP4 and IP6, however, in this class we
will only be working with IP4.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Socket Programming Tutorial: TCP Client and Server and more Study notes Computer Science in PDF only on Docsity!

Introduction to Socket Programming

Part II

Code snipet:

struct sockaddr_in myAddressStruct;

//Fill in the address information into myAddressStruct here, (will be explained in detail shortly)

connect(socket_file_descriptor, (struct sockaddr *) &myAddressStruct, sizeof(myAddressStruct));

Now lets discuss how to fill in the sockaddr_in structure:

struct sockaddr_in{

sa_family_t sin_family /Address/Protocol Family/ unit16_t sin_port /* Port number --Network Byte Order-- / struct in_addr sin_addr /A struct for the 32 bit IP Address / unsigned char sin_zero[8] /Just ignore this it is just padding*/ };

struct in_addr{ unit32_t s_addr /*32 bit IP Address --Network Byte Order-- */ };

For the sa_family variable sin_family always use the constant: PF_INET or AF_INET ***Always initialize address structures with bzero() or memset() before filling them in *** Make sure you use the byte ordering functions when necessary for the port and IP address variables otherwise there will be strange things a happening to your packets

Converting between dotted decimal strings and Network Address values (Read pgs 70 – 74 Stevens)

To convert a string dotted decimal IP4 address to a NETWORK BYTE ORDERED 32 bit value use the functions:

  • inet_addr()
  • inet_aton()

To convert a 32 bit NETWORK BYTE ORDERED to a IP4 dotted decimal string use:

  • inet_ntoa()

Read Stevens also for more recent functions which work with both IP4 and IP6, however, in this class we will only be working with IP4.

6.) Outline of a TCP Client (Read Chapter 4 in Stevens)

Step 1: Create a socket : int socket(int family, int type, int protocol) (Read pgs 86-88 Stevens)

Creating a socket is in some ways similar to opening a file. This function creates a file descriptor and returns it from the function call. You later use this file descriptor for reading, writing and using with other socket functions

Parameters: family: AF_INET or PF_INET (These are the IP4 family) type: SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP) protocol: IPPROTO_TCP (for TCP) or IPPROTO_UDP (for UDP) or use 0

Step 2: Binding a socket: This is unnecessary for a client, what bind does is (and will be discussed in detail in the server section) is associate a port number to the application. If you skip this step with a TCP

your application. Read Stevens very carefully, especially pages 77-78 to understand more how to properly use read.

recv/send (Read pgs 354 – 357 Stevens )

8.) Shutting down sockets Ater you are finished reading and writing to your socket you most call the close system call on the socket file descriptor just as you do on a normal file descriptor otherwise you waste system resources.

The close() function: int close(int filedescriptor);

The shutdown() function You can also shutdown a socket in a partial way which is often used when forking off processes. You can shutdown the socket so that it won’t send anymore or you could also shutdown the socket so that it won’t read anymore as well. This function is not so important now but will be discussed in detail later. You can look at the man pages for a full description of this function.

9.) Outline of a TCP Server (Read Chapter 4 in Stevens).

Step 1: Creating a socket: Same as in the client Step 2: Binding an address and port number (Read pgs 91 – 93 Stevens) int bind(int socket_file_descriptor, const struct sockaddr * LocalAddress, socklen_t AddressLength);

We need to associate an IP address and port number to our application. A client that wants to connect to our server needs both of these details in order to connect to our server. Notice the difference between this function and the connect() function of the client. The connect function specifies a remote address that the client wants to connect to, while here, the server is specifying to the bind function a local IP address of one of its Network Interfaces and a local port number.

**Again make sure that you cast the structure as a generic address structure in this function **

You also do not need to find information about the IP addresses associated with the host you are working on. You can specify: INNADDR_ANY to the address structure and the bind function will use on of the available (there may be more than one) IP addresses. Read Stevens page 92 for more details.

Step 3: Listen for incoming connections (Read pgs 93 – 99 Stevens)

Gathering information about your local host: int uname(struct utsname * buf)

struct utsname{ char sysname[SYS_NMLN]; char nodename[SYS_NMLN]; char release[SYS_NMLN]; char version[SYS_NMLN]; char machine[SYS_NMLN]; char domainname[SYS_NMLN]; };

see also :

  • gethostname()
  • getdomainname()

Gathering information about a remote host: struct hostent *gethostbyname(const char *name)

struct hostent{ char h_name; /Official name of host/ char ** h_aliases; /Alias list/ int h_addrtype; /host address type / int h_length; /length of address/ char ** h_addr_list; /list of addresses*/ };

Example:

struct hostent *ptr;

ptr = gethostbyname(www.ucr.edu);

for (int i = 0; ptr->h_aliases[i] != NULL; ++i) printf(“alias = %s\n”, ptr->h_aliases[i]); if (ptr->h_addrtype == AF_INET) for (int i = 0; ptr->h_addr_list[i] != NULL; ++i) printf(“Address = %s\n”, *(struct in_addr *) ptr->h_addr_list[i]);

12.) Summary of Functions

For specific and up-to-date information about each of the following functions, please use the online man pages and Steven’s Unix Network Programming Vol. I.

Socket creation and destruction:

  • socket()
  • close()
  • shutdown()

Client:

  • connect()
  • bind()

Server:

  • accept()
  • bind()
  • listen()

Data Transfer:

  • send()
  • recv()
  • write()
  • read()

Miscellaneous:

  • bzero()
  • memset()

Host Information:

  • uname()
  • gethostbyname()
  • gethostbyaddr()

Address Conversion:

  • inet_aton()
  • inet_addr()
  • inet_ntoa()