Question
To get started, please write two programs which run on the CSE machines (ie cse01, cse02, etc). One will be a server, one will be
To get started, please write two programs which run on the CSE machines (ie cse01, cse02, etc). One will be a server, one will be a client. PROGRAM MUST BE IN C++
The server:
The server needs to create a TCP/IP socket. This means a connected Internet socket. Look at the socket(2) system call and study the parameters. If you still don't see how to create the socket, please ask. I just want everyone to try on their own first.
Servers need to bind addresses. What address will the server respond to? You have the option of having the system fill in your local IP address. You need to select a port to operate on. A port is a way of indicating the application-level protocol. Please pick a random number between 10K and 64K and use that for both your client and server. Both programs need to agree on this port number. If you get an error about "address already in use" then pick another port number.
After binding, you need to setup a connection queue to hold incoming clients. It is possible for multiple clients to arrive at the same time, and you don't want to miss them. While your server should be quick about connecting to these clients, in practice one creates a small queue. The listen(2) system call is used to create that queue.
The server should wait in a loop for new connections. Servers usually have infinite loops for this, with some other way of exiting. Terminating the program with ^C is an option. Inside of the loop, the server should call accept(2). Note that this will put the server to sleep until a client arrives. When a client tries to connect to the server, the server will be woke and the accept() call completes. Accept provides the server with the address of the new client.
The server should process any data sent from the client. Real servers can have extended conversations with a client. You just need to read one string. You may assume that the string will fit in a 1500 byte packet.
Print that string out, along with the IP address of the client (in dotted decimal notation). See inet_ntoa(3).
When the conversation is done, the server should use close(2) or shutdown(2) to end the conversation. Accept will open a new connection for the next client. You do not want to leave sockets open when the client disconnects, or you could fill up your open file table.
The client:
This is much simpler. The client needs to create a socket, just like the server. The client needs to create a struct sockaddr structure with the server's address. You may hard-code this address. Later we will consider how you could discover that at runtime.
The client should connect(2) to the server. Once connect returns successfully, you may write to the socket. Be sure to close the socket when you are done.
The client only needs to send one string per run, and it may then exit. The server sticks around until you shut it down.
Sample output
received from 127.0.0.1: hello, network
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started