In C++: I need to make a socket program with a server and a client file. First t
ID: 3790400 • Letter: I
Question
In C++:
I need to make a socket program with a server and a client file.
First the client creates a TCP connection with the server using <server_address> as the server address and <n_port> as the negotiation port on the server . The client sends a request to get the random port number on the server where the client will send the actual data. For simplicity, the client will send the characters 123 (as a single message) to initiate the negotiation with the server.
Once the server receives this request in the negotiation port, the server will reply back with a random port number<r_port> between 1024 and 65535 (inclusive) where it will be listening for the expected data. The server will then write to screen “Negotiation detected. Selected the following random port <r_port>”. Both the client and server must close their sockets once the negotiation stage has completed.
Stage 2: Transaction using UDP sockets – In this stage, the client creates a UDP socket to the server in<r_port> and sends the data. This data is assumed to be 8-bit ASCII (assuming standard 8-bit encoding for each character, so 1 byte per character) and may be of arbitrary finite length of at least 1 byte. The file is sent over the channel in chunks of 4 characters of the file per UDP packet (a character includes white space, exclamation points, commas, etc.). An exception to this rule occurs when the 2 Client Server Random port Negotiation Send data Acknowledgement Figure 2 Send data Acknowledgement end of the file is reached and, in that case, less than 4 characters of the file can be sent in the next packet. We call each such chunk of the file a payload.
The packet may contain other information in addition to the payload, if you deem it useful (for example, to indicate the end of the file). After each packet is sent, the client waits for an acknowledgement from the server that comes in the form of the most recent transmitted payload in capital letters. These acknowledgements are output to the screen on the client side as one line per packet (the client does not need to write these acknowledgements to any file).
Once the file has been sent and the last acknowledgement received, the client closes its ports and terminates automatically; that is, it must determine the end of the file.
On the other side, the server receives the data and writes it (does not append) to file using the filename “output.txt”. After each received packet, the server uses the UDP socket to send back an acknowledgement to the client that is the most recent received payload in capital letters. The server does not write the received data to screen.
Once the last acknowledgement has been sent, the server closes all ports and terminates automatically (that is, it must determine that end of the transmission has occurred from the client).
client file name should be "client" and takes command line nputs <server address>,<n_port>,and <file_name>.
server file name should be server and takes command line inputs <n_port>.
Explanation / Answer
client
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /*port*/
int
main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE];
//basic check of the arguments
//additional checks can be inserted
if (argc !=2) {
perror("Usage: TCPClient <IP address of the server");
exit(1);
}
//Create a socket for the client
//If sockfd<0 there was an error in the creation of the socket
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
perror("Problem in creating the socket");
exit(2);
}
//Creation of the socket
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr(argv[1]);
servaddr.sin_port = htons(SERV_PORT); //convert to big-endian order
//Connection of the client to the socket
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
perror("Problem in connecting to the server");
exit(3);
}
while (fgets(sendline, MAXLINE, stdin) != NULL) {
send(sockfd, sendline, strlen(sendline), 0);
if (recv(sockfd, recvline, MAXLINE,0) == 0){
//error: server terminated prematurely
perror("The server terminated prematurely");
exit(4);
}
printf("%s", "String received from the server: ");
fputs(recvline, stdout);
}
exit(0);
}
server
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /*port*/
#define LISTENQ 8 /*maximum number of client connections */
int main (int argc, char **argv)
{
int listenfd, connfd, n;
socklen_t clilen;
char buf[MAXLINE];
struct sockaddr_in cliaddr, servaddr;
//creation of the socket
listenfd = socket (AF_INET, SOCK_STREAM, 0);
//preparation of the socket address
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind (listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen (listenfd, LISTENQ);
printf("%s ","Server running...waiting for connections.");
for ( ; ; ) {
clilen = sizeof(cliaddr);
connfd = accept (listenfd, (struct sockaddr *) &cliaddr, &clilen);
printf("%s ","Received request...");
while ( (n = recv(connfd, buf, MAXLINE,0)) > 0) {
printf("%s","String received from and resent to the client:");
puts(buf);
send(connfd, buf, n, 0);
}
if (n < 0) {
perror("Read error");
exit(1);
}
close(connfd);
}
//close listening socket
close (listenfd);
}