0% found this document useful (0 votes)
78 views

TCP Client Server

This C++ code implements a TCP receiver that listens on port 5000 for incoming connection requests. When a connection is established, it receives messages from the client and prints them out. It can also send messages back to the client. The receiver uses socket, bind, listen, accept and recv functions to set up the passive socket and receive data. It will continue receiving/sending messages in a loop until the client sends the word "bye", at which point the connection is closed.

Uploaded by

sujuki794
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

TCP Client Server

This C++ code implements a TCP receiver that listens on port 5000 for incoming connection requests. When a connection is established, it receives messages from the client and prints them out. It can also send messages back to the client. The receiver uses socket, bind, listen, accept and recv functions to set up the passive socket and receive data. It will continue receiving/sending messages in a loop until the client sends the word "bye", at which point the connection is closed.

Uploaded by

sujuki794
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// tcp-receiver.cpp TCP version of receiver.cpp #include <iostream> #include <string> #include <cstdlib> #include <sys/socket.

h> sockaddr, #include #include #include #include #include <netinet/in.h> <arpa/inet.h> <unistd.h> <csignal> <cstdlib> // // // // // // // // // // cout, cerr, endl string exit() socket(), bind(), listen(), accept(), recv(), struct socklen_t, AF_INET, SOCK_STREAM, INADDR_ANY struct sockaddr_in htons(), ntohs() close() for signal() for exit()

using namespace std; const unsigned int PORT = 5000; // receiver listens on this port const int BUFLEN = 256; // buffer length (size) int main(int argc, char *argv[]) { int sockfd; // socket descriptor struct sockaddr_in receiver; // receiver address information struct sockaddr_in sender; // sender address information char buf[BUFLEN + 1]; // message buffer string msg; // message int bytes; // number of bytes received socklen_t sockaddrlen; // sizeof(struct sockaddr), used by accept() int asocket; // active socket // create socket (passive socket) if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { cerr << "Error: socket() failed" << endl; exit(1); } // receiver address information receiver.sin_family = AF_INET; // address family receiver.sin_port = htons(PORT); // port number receiver.sin_addr.s_addr = INADDR_ANY; // IP address // assign receiver address to socket if(bind(sockfd, (struct sockaddr *)&receiver, sizeof(struct sockaddr)) == -1) { cerr << "Error: bind() failed" << endl; exit(1); } while(1){ // listen for connection request listen(sockfd, 100); // non-blocking call (i.e., call returns) // 2nd parameter defines the max size of the queue of pending connections cout << "Waiting for message on port " << PORT << "..." << endl; sockaddrlen = sizeof(struct sockaddr); // accept connection if((asocket = accept(sockfd, (struct sockaddr *) &sender, &sockaddrlen)) < 0) {

cerr << "Error: Can't accept connection" << endl; exit(1); } else cout << "Connection Established..." << endl; string message = "Connected"; while(1) { // receive message if((bytes = send(asocket, message.c_str(), message.length(), 0)) == -1) { cerr << "Error: send() failed" << endl; exit(1); } if((bytes = recv(asocket, buf, BUFLEN, 0)) == -1) { cerr << "Error: recv() failed" << endl; exit(1); } buf[bytes] = '\0'; msg = buf; // Note: recv() returns 0 if connection has been closed by client cout << bytes << " bytes received from " << inet_ntoa(sender.sin_addr) << " port " << ntohs(sender.sin_port) << endl; cout << "client message: \"" << msg << "\"" << endl; if(msg=="bye") { message="bye"; bytes = send(asocket, message.c_str(), message.length(), 0); break; } else { getline(cin, message); } if(message=="bye") { bytes = send(asocket, message.c_str(), message.length(),0); break; } } } close(asocket); // close active socket close(sockfd); // close passive socket return 0; }

You might also like