Download presentation
Presentation is loading. Please wait.
1
Client-Server Network Programming Session 1: Introduction to Sockets and socket types A workshop by Dr. Junaid Ahmed Zubairi Department of Computer Science State University of New York at Fredonia
2
Workshop Outline 1. Client-Server Mechanisms 2. How the Applications work 3. Introduction to sockets 4. Socket types 5. Programming with sockets 6. Concurrent Processing 7. Programming project
3
Workshop References 1. “Computer Networking: A Top Down Approach Featuring the Internet” Kurose and Ross, Addison Wesley 2001 2. “Internetworking with TCP/IP Vol 3: Client- Server Programming and Applications” Comer and Stevens, Prentice Hall 2001 3. “Hands on Networking” Doug Comer 2 nd edition Pearson Prentice Hall 2005 4. “An Introduction to Network Programming with Java” Jan Graba Pearson Addison Wesley 2003
4
Client-Server Mechanisms Let us assume that a person is trying to start two programs on separate networked machines (M1 and M2) and these programs must communicate with each other When the person starts the first program on machine M1, this program sends a message to the program on machine M2 Since the computer works much faster than human beings, the program on M1 waits a few milliseconds for response from M2 and then gives up displaying an error message
5
Client-Server Mechanisms Meanwhile, our friend reaches M2 and types the second program name to start it on M2 The second program waits for a message from M1 for a few milliseconds and then concludes that the first program is not active yet. It displays an error message and exits. Our friend would keep starting the programs but they will never be able to communicate.
6
Client-Server Mechanisms The solution is to let one program open up communications and wait indefinitely for a message from the other program The first program to start running is called the server. The server must listen for any message from the client The other program is called the client and the client is supposed to initiate the communication by sending a message to the server
7
Client-server paradigm Typical network app has two pieces: client and server application transport network data link physical application transport network data link physical Client: initiates contact with server (“speaks first”) typically requests service from server, for Web, client is implemented in browser; for e-mail, in mail reader Server: provides requested service to client e.g., Web server sends requested Web page, mail server delivers e- mail request reply
8
Introduction to Sockets A machine on the network may be running various network servers (Web, Email, FTP, DNS etc.) Many different clients may contact this machine, all requesting various services The machine services the clients correctly based on the port numbers
9
application transport network M P2 application transport network Multiplexing/demultiplexing segment - unit of data exchanged between transport layer entities aka TPDU: transport protocol data unit receiver H t H n Demultiplexing: delivering received segments to correct app layer processes segment M application transport network P1 MMM P3 P4 segment header application-layer data
10
Multiplexing/demultiplexing multiplexing/demultiplexing: based on sender, receiver port numbers, IP addresses source, dest port #s in each segment well-known port numbers for specific applications gathering data from multiple app processes, enveloping data with header (later used for demultiplexing) source port #dest port # 32 bits application data (message) other header fields TCP/UDP segment format Multiplexing:
11
Multiplexing/demultiplexing: examples host A server B source port: x dest. port: 23 source port:23 dest. port: x port use: simple telnet app Web client host A Web server B Web client host C Source IP: C Dest IP: B source port: x dest. port: 80 Source IP: C Dest IP: B source port: y dest. port: 80 port use: Web server Source IP: A Dest IP: B source port: x dest. port: 80
12
Well-Known Ports The application servers listen on well-known ports or “reserved” ports so that every user can connect to them. Some well known ports are as under: HTTP: 80 FTP: 21 SMTP: 25 POP3: 110 More ports in RFC1700 (www.ietf.org)
13
Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: unreliable datagram reliable, byte stream- oriented a host-local, application- created/owned, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another (remote or local) application process socket Goal: learn how to build client/server application that communicate using sockets
14
Socket-programming Socket: a door between application process and end-end-transport protocol (UDP or TCP) TCP service: reliable transfer of bytes from one process to another process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server internet
15
Socket programming with TCP Client must contact server server process must first be running server must have created socket (door) that welcomes client’s contact Client contacts server by: creating client-local TCP socket specifying IP address, port number of server process When client creates socket: client TCP establishes connection to server TCP When contacted by client, server TCP creates new socket for server process to communicate with client allows server to talk with multiple clients TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint
16
Socket API Each socket is identified by its socket descriptor The OS allocates a new data structure to hold necessary information on creation of a socket A passive socket listens for incoming messages. An active socket is used by a client to initiate connection
17
Socket API For TCP/IP communications, the programs should use predefined structure “sockaddr_in” Major system calls in Socket API are as follows: socket() to create a socket connect() to establish active connection to a server send() to transfer data recv() to receive data close() to terminate the connection
18
Sockaddr_in struct sockaddr_in { u_char sin_len; //total length u_short sin_family; //type of address u_short sin_port; //protocol port no. struct in_addr sin_addr; //ip address char sin_zero[8]; //unused }
19
Socket API Client side: socket connect send recv close Server side: socket bind listen accept recv send close Programs must #include and
20
Getting Started with Sockets #include int main(void) { int s; s = socket(PF_INET, SOCK_STREAM, 0); printf(“I just got a socket created\n”); return 1; }
21
Getting Started With Sockets The socket() call takes 3 arguments socket(domain,type,protocol) The first argument is the domain. PF_INET for network sockets PF_UNIX for system sockets The second argument is the socket type SOCK_DGRAM is for UDP socket SOCK_STREAM is for TCP socket The third argument is left 0 so that the system can select the correct protocol System returns a descriptor that can be used to refer to the socket later
22
Getting Started With Sockets Once a socket is created, we should be able to connect to a remote machine If we are on the client side, we need to get the server’s IP address and then try to connect to the server
23
Client Side Get remote host information by using the following statements. Do not forget to #include and struct hostent * ph; //remote host name and info struct sockaddr_in sa; //holds IP address and protocol port memset(&sa, 0, sizeof(sa)); //zero out the sa struct if ((ph = gethostbyname (“www.cs.fredonia.edu”)) == NULL) { printf(“error in gethostbyname\n”); exit(1); } We tried to get the IP address by using DNS service. If it failed, the program will exit with a nonzero error code. hostent structure is defined in netdb.h
24
Client connects to a web server //now ph has the IP address of the server, copy it to sa memcpy((char *) &sa.sin_addr,ph->h_addr, ph->h_length); //Fill out the sin_port information by converting the port number to network byte order sa.sin_port = htons((u_short) 80); sa.sin_family = ph->h_addrtype; if (connect(s, &sa, sizeof(sa) < 0)) { perror(“connect error\n”);exit(1);} Network byte order is a neutral order for sending numerical quantities. It is used because PC’s store the numbers in opposite way to mainframes and UNIX workstations.
25
Client requests a web page Now client requests a web page from the server. Let us check out the sample source code and try to understand as to what is going on
26
Client Requests a Web Page #include #define BUFLEN 1024 char req[40] = "GET /~zubairi/index.html HTTP/1.0\n\n"; char buf[BUFLEN]; char *bptr; int n; int buflen; int count=0; main() { int s; struct hostent * ph; //remote host name and info
27
Sample Source Code struct sockaddr_in sa; //holds IP address and protocol port req[39] = '\0'; printf("%s",req); s = socket(PF_INET, SOCK_STREAM, 0); printf("Socket was created\n"); memset(&sa, 0, sizeof(sa)); //zero out the sa struct if ((ph = gethostbyname ("www.cs.fredonia.edu")) == NULL) { printf("error in gethostbyname\n"); exit(1); } //now ph has the IP address of the server, copy it to sa memcpy((char *) &sa.sin_addr,ph->h_addr, ph->h_length);
28
Sample Source Code //Fill out the sin_port information by converting the //port number to network byte order sa.sin_port = htons((u_short) 80); sa.sin_family = ph->h_addrtype; if (connect(s, &sa, sizeof(sa)) < 0) { perror("connect error\n");exit(1);} else printf("connected to web server\n"); bptr = buf; buflen = BUFLEN; if (send (s, req, strlen(req), 0)<=0) perror("send error\n"); else printf("Sent to the server\n");
29
Sample Source Code printf("Now starting to receive\n"); While ( (n = recv(s,bptr,buflen,0) ) > 0) { printf("Still receiving\n"); count++; } if (!count) printf("Error in receive\n"); else printf("received all \n"); printf("%s\n",bptr); }
30
Web client code C strings are null terminated (\0) Request to get the web page is sent to the web server at port 80 Source code receives and displays all pieces received You can also receive and display piece by piece as shown
31
Web client code while ( (n = recv(s,bptr,buflen,0) ) > 0) { printf("%s",bptr); //print the received data bptr = bptr+n; //advance the pointer to buffer beyond received bytes buflen = buflen –n; //reduce the available buffer length accordingly }
32
TCP Server Following is an example server program from Dr. Comer’s ftp site. It waits for connection from the client, sends a short message to it and then closes connection
33
TCP Server Initialization #include #definePROTOPORT5193 // port number #defineQLEN6// size of request queue intvisits = 0;// counts client connection
34
TCP Server Initialization main(argc, argv) intargc; char*argv[]; { structhostent *ptrh; /* pointer to a host table entry*/ structprotoent *ptrp; /* pointer to a protocol table entry*/ structsockaddr_in sad; /* structure to hold server's address*/ structsockaddr_in cad; /* structure to hold client's address*/ intsd, sd2; /* socket descriptors*/ intport; /* protocol port number*/ intalen; /* length of address*/ charbuf[1000]; /* buffer for string the server sends*/
35
Setting Port No. for TCP Server memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure*/ sad.sin_family = AF_INET; /* set family to Internet*/ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address*/ /* Check command-line argument for protocol port and extract it*/ if (argc > 1) {/* if argument specified*/ port = atoi(argv[1]);/* convert argument to binary*/ } else port = PROTOPORT; /* else use default port number*/ if (port > 0)/* test for illegal value*/ sad.sin_port = htons((u_short)port); else {/* print error message and exit*/ fprintf(stderr,"bad port number %s\n",argv[1]); exit(1); }
36
Map Protocol and Create a Socket /* Map TCP transport protocol name to protocol number */ if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) { fprintf(stderr, "cannot map \"tcp\" to protocol number"); exit(1); } /* Create a socket */ sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto); if (sd < 0) { fprintf(stderr, "socket creation failed\n"); exit(1); }
37
Bind to Socket and Set Queue /* Bind a local address to the socket */ if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) { fprintf(stderr,"bind failed\n"); exit(1); } /* Specify size of request queue */ if (listen(sd, QLEN) < 0) { fprintf(stderr,"listen failed\n"); exit(1); }
38
Accept Requests and Fulfill while (1) { alen = sizeof(cad); if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) { fprintf(stderr, "accept failed\n"); exit(1); } visits++; sprintf(buf,"This server has been contacted %d time%s\n", visits,visits==1?".":"s."); send(sd2,buf,strlen(buf),0); close(sd2); }
39
Lab 1 First compile and run the server Next modify the client code to set the IP address of the server to the correct value and port number to the correct port. Now the client can be started Each time the client runs, the server keeps incrementing the count of clients served
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.