TCP Client_Server PowerPoint

19
by Somebody

Transcript of TCP Client_Server PowerPoint

Page 1: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 1/19

by

Somebody

Page 2: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 2/19

1. Introduction

2.Elements of Client Server Computing3.TCP Socket

4.TCP Sockets• Implementing a Client• Implementing a Server•Sample Examples

5.Conclusion

Page 3: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 3/19

• Internet and WWW have emerged as globalubiquitous 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 activitiesare getting completely centered aroundthe Internet.

Page 4: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 4/19

a client, a server, and network 

Client machine

Server machine

Network 

Page 5: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 5/19

Page 6: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 6/19

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

communication link between two programs

running on the network.

• A server application normally listens to a

specific port waiting for connection requests

from a client. When a connection request

arrives, the client and the server establish adedicated connection over which they can

communicate.

Page 7: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 7/19

• During the connection process, the client is

assigned a local port number, and binds

a socket to it. The client talks to the server bywriting 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 originalport).

• The server also binds a socket to its local port

and communicates with the client by reading

from and writing to it.

Page 8: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 8/19

After initialization, a SOCKET object must be instantiated for useby 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;} 

Page 9: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 9/19

2. Connect the socket to the address of the server using theconnect() 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;

}

Page 10: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 10/19

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);

Page 11: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 11/19

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");

elseprintf("recv failed with error: %d\n", WSAGetLastError());

Page 12: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 12/19

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;

}

Page 13: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 13/19

• 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 infiniteloop.

• After a connection is established, call  fork() to create a newprocess.

• 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 indicatedby doprocessing() returning, this process simply exits.

• The parent process closes newsockfd . Because all of this code isin an infinite loop, it will return to the accept statement to waitfor the next connection.

Page 14: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 14/19

• 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;

}

Page 15: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 15/19

2. Bind the socket to an address using the bind() system call. Fora server socket on the Internet, an address consists of a portnumber 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;

}

Page 16: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 16/19

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;

}

Page 17: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 17/19

4. Accept a connection with the accept() system call. This calltypically blocks until a client connects with the server.

// Accept a client socket and Error messageClientSocket = accept(ListenSocket, NULL, NULL);

if (ClientSocket == INVALID_SOCKET)

{

printf("accept failed with error: %d\n", WSAGetLastError());

closesocket(ListenSocket);WSACleanup();

system("pause");

return 1;

}

Page 18: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 18/19

5. Send and receive data using the read(), and write() systemcalls.// Receive until the peer shuts down the connection

do {

// RECEIVE MESSAGE FROM THE CLIENTiResult = 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;

}

Page 19: TCP Client_Server PowerPoint

7/27/2019 TCP Client_Server PowerPoint

http://slidepdf.com/reader/full/tcp-clientserver-powerpoint 19/19

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

challenging.

• Client server application is basically used forcommunication 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!!!