TCP Sockets Programming: Creating Server and Client, Study notes of Computer Science

The code and usage examples for creating a tcp server and client using sockets in c. The server creates a socket, binds it to a port, and listens for incoming client connections. Each client sends a message to the server and receives an echoed message back. Examples of both minimal and comprehensive server and client implementations.

Typology: Study notes

Pre 2010

Uploaded on 02/12/2009

koofers-user-cvo-1
koofers-user-cvo-1 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Network Programming-TCP Sockets
(lecture programs)
Bare Minimum Clients/Servers
Each server:
¾ Creates a server socket,
¾ Binds it to a fixed port (10203),
¾ Receives a message from a client and displays it.
Each client (udp or tcp):
¾ Creates a client socket,
¾ Sends "Hi" repeatedly out of this socket to the server.
tcp Server and Client
tcpServer.c
#include "def"
main()
{
int sd, psd;
struct sockaddr_in name;
char buf[1024];
int cc;
sd = socket (AF_INET,SOCK_STREAM,0);
name.sin_family = AF_INET;
name.sin_addr.s_addr = htonl(INADDR_ANY);
name.sin_port = htons(10203);
bind( sd, (SA *) &name, sizeof(name) );
listen(sd,1);
pf3
pf4
pf5

Partial preview of the text

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

Network Programming-TCP Sockets (lecture programs)

Bare Minimum Clients/Servers

Each server:

¾ Creates a server socket , ¾ Binds it to a fixed port (10203), ¾ Receives a message from a client and displays it.

Each client (udp or tcp):

¾ Creates a client socket , ¾ Sends "Hi" repeatedly out of this socket to the server.

tcp Server and Client

tcpServer.c

#include "def" main() { int sd, psd; struct sockaddr_in name; char buf[1024]; int cc;

sd = socket (AF_INET,SOCK_STREAM,0);

name.sin_family = AF_INET; name.sin_addr.s_addr = htonl(INADDR_ANY); name.sin_port = htons(10203);

bind( sd, (SA *) &name, sizeof(name) );

listen(sd,1);

psd = accept(sd, 0, 0);

for(;;) { cc=recv(psd,buf,sizeof(buf), 0) ; if (cc == 0) exit (0); buf[cc] = NULL; printf("message received: %s\n", buf); } }

Usage example:

% tcpServer

tcpClient.c

#include "def"

main(argc, argv ) int argc; char *argv[];

{ int sd; struct sockaddr_in server; struct hostent *hp, *gethostbyname();

sd = socket (AF_INET,SOCK_STREAM,0);

server.sin_family = AF_INET; hp = gethostbyname(argv[1]); bcopy ( hp->h_addr, &(server.sin_addr.s_addr), hp-

h_length); server.sin_port = htons(10203);

connect(sd, (SA *) &server, sizeof(server));

for (;;) { send(sd, "HI", 2, 0 ); printf("sent HI\n"); sleep(2); } }

/get TCPServer1 Host information: NAME and INET ADDRESS/

gethostname(ThisHost, MAXHOSTNAME); printf("TCP/Server running at host NAME: %s\n", ThisHost); hp = gethostbyname(ThisHost)); bcopy ( hp->h_addr, &(server.sin_addr), hp->h_length); printf(" (TCP/Server INET ADDRESS is: %s )\n", inet_ntoa(server.sin_addr));

/* Construct name of socket / server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); if (argc == 1) server.sin_port = htons(0); else { server.sin_port = htons(atoi(argv[1])); } / Create socket on which to send and receive */ sd = socket (AF_INET,SOCK_STREAM,0); bind( sd, (SA *) &server, sizeof(server);

/* get port information and prints it out */ length = sizeof(server); getsockname (sd, (SA *)&server,&length); printf("Server Port is: %d\n", ntohs(server.sin_port));

/* accept TCP connections & fork a process to serve each client */ listen(sd,4); fromlen = sizeof(from); for(;;){ psd = accept(sd, (SA *)&from, &fromlen); childpid = fork(); if ( childpid == 0) { close (sd); EchoServe(psd, from); } else{ printf("My new child pid is %d\n", childpid); close(psd); } } }

EchoServe(psd, from) int psd; struct sockaddr_in from; { .... /* print client information */ printf("Serving %s:%d\n",inet_ntoa(from.sin_addr),

ntohs(from.sin_port));

hp = gethostbyaddr((char *) &from.sin_addr.s_addr, sizeof(from.sin_addr.s_addr),AF_INET));

printf("(Name is : %s)\n", hp->h_name);

/* get data from clients and send it back */ for(;;){ rc=recv(psd, buf, sizeof(buf), 0); if (rc > 0){ buf[rc]=NULL; printf("Received: %s\n", buf); printf("From TCP/Client: %s:%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port)); printf("(Name is : %s)\n", hp->h_name); send(psd, buf, rc, 0); }else { printf("Disconnected..\n"); close (psd); exit(0); } } }

Usage example:

% TCPServer % TCPServer1 10101

TCPClient.c

main( ..) { ....

/*get TCPClient1 Host information, NAME and INET ADDRESS */ gethostname(ThisHost, MAXHOSTNAME); printf("TCP/Cleint running at host NAME: %s\n", ThisHost); hp = gethostbyname(ThisHost));

/* get data from USER, send it SERVER */

GetUserInput() { for(;;) { printf("Type anything followed by RETURN, or type CTRL-D to exit\n"); rc=read(0,buf, sizeof(buf)); if (rc == 0) break; send(sd, buf, rc, 0); } printf ("EOF... exit\n"); close(sd); kill(getppid(), 9); exit (0); }

Usage example:

% TCPClient1 localhost 10101