You are on page 1of 19

by

Somebody

1. Introduction 2. Elements of Client Server Computing 3. TCP Socket 4. TCP Sockets Implementing a Client Implementing a Server 5. Conclusion
Sample Examples

Internet and WWW have emerged as global

ubiquitous media for communication and changing the way we conduct science, engineering, and commerce. They also changing the way we learn, live, enjoy, communicate, interact, engage, etc. It appears like the modern life activities are getting completely centered around the Internet.

a client, a server, and network

Network

Client machine

Server machine

A socket is one end-point of a two-way

communication link between two programs running on the network. specific port waiting for connection requests from a client. When a connection request arrives, the client and the server establish a dedicated connection over which they can communicate.

A server application normally listens to a

During the connection process, the client is

assigned a local port number, and binds a socket to it. The client talks to the server by writing to the socket and gets information from the server by reading from it. Similarly, the server gets a new local port number (it needs a new port number so that it can continue to listen for connection requests on the original port). and communicates with the client by reading from and writing to it.

The server also binds a socket to its local port

After initialization, a SOCKET object must be instantiated for use by the client.

These are the steps to follow:


1. Create a socket with the socket() system call.
// Create a SOCKET for connecting to server ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
// Error message for an Invalid Socket if (ConnectSocket == INVALID_SOCKET)

{
printf("socket failed with error: %ld\n", WSAGetLastError()); WSACleanup(); system("pause"); return 1;

2. Connect the socket to the address of the server using the connect() socket call.
// Connect to server. iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); // Socket Error if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); ConnectSocket = INVALID_SOCKET; continue; }

3. Send data from the user and to the server.


//GET THE DATA FROM THE USER printf(" Enter Data to send or EXIT : "); scanf("%s", sendbuf); //SEND THE DATA TO THE SERVER iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); printf("Bytes Sent: %ld\n", iResult);

4. Receive the data from the user and to the server.


///Start Receive//// iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); if ( iResult > 0 ) printf("Bytes received: %d %s\n", iResult, recvbuf); else if ( iResult == 0 ) printf("Connection closed\n"); else printf("recv failed with error: %d\n", WSAGetLastError());

5. Shutdown of the program // shutdown the connection since no more data will be sent iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed with error: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); system("pause"); return 1; }

To allow the server to handle multiple simultaneous connections,

we make the following changes in the above code: Put the accept statement and the following code in an infinite loop. After a connection is established, call fork() to create a new process. The child process will close sockfd and call doprocessing function, passing the new socket file descriptor as an argument. When the two processes have completed their conversation, as indicated by doprocessing() returning, this process simply exits. The parent process closes newsockfd. Because all of this code is in an infinite loop, it will return to the accept statement to wait for the next connection.

These are the steps to follow to connect with the server:


1. Create a socket with the socket() system call.
// Create a SOCKET for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

// Error for an Invalid Socket


if (ListenSocket == INVALID_SOCKET) { printf("socket failed with error: %ld\n", WSAGetLastError()); freeaddrinfo(result); WSACleanup(); system("pause"); return 1; }

2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
// Setup the TCP listening socket and Error message iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen); if (iResult == SOCKET_ERROR) { printf("bind failed with error: %d\n", WSAGetLastError()); freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); system("pause"); return 1; }

3. Listen for connections with the listen() system call.


///Listen Socket and Error message iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { printf("listen failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); system("pause"); return 1; }

4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
// Accept a client socket and Error message ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { printf("accept failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); system("pause"); return 1; }

5. Send and receive data using the read(), and write() system calls.
// Receive until the peer shuts down the connection do { // RECEIVE MESSAGE FROM THE CLIENT iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); //Do a string comparison //If receive buff = exit set iResult to 0 if( strcmp(recvbuf, "EXIT")==0) iResult = 0; if (iResult > 0) { printf("Bytes received: %d %s\n", iResult, recvbuf); // Echo the buffer back to the sender iSendResult = send( ClientSocket, recvbuf, iResult, 0 ); if (iSendResult == SOCKET_ERROR) { printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); system("pause"); return 1; }

Programming client/server applications in C++ is fun and


challenging.

Client server application is basically used for

communication from one system to another at which one client can communicate with more then one server at a time.

Keywords:
Client, server, TCP/IP, port number, sockets, C# sockets

Any Questions!!!

You might also like