Professional Documents
Culture Documents
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
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.
Network
Client machine
Server machine
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.
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.
After initialization, a SOCKET object must be instantiated for use by the client.
{
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; }
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; }
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.
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; }
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; }
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!!!