You can learn a lot about the design of client-server protocols by reviewing the
ID: 3848907 • Letter: Y
Question
You can learn a lot about the design of client-server protocols by reviewing the implementation of one. And you can develop an even greater understanding by extending or updating that implementation. For this Assignment, you will modify an existing client-server protocol to implement the Internet’s finger protocol. To prepare: Download the existing program contained in Week6_Echo.zip. This file unzips into a NetBeans project that includes the source code for two Java programs, a client program, and a server program. The two programs implement the Internet’s echo protocol. Start up NetBeans. Open the Week6_Echo project you just downloaded and unzipped. By Day 7, modify the client and the server so that they implement the Internet’s finger protocol. The response from the server does not need to reflect actual user data, but you must base the response on the name provided in the client’s request. Save and submit your Assignment as a ".zip" file. Retain your implementation, as you will be using it again in Week 7.
echoclient.java:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class EchoClient {
public static void main(String[] args) throws IOException {
int b;
Socket socket = new Socket(args[0], 7000);
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
System.out.println("The socket is connected the server.");
System.out.println("... local socket address is " + socket.getLocalSocketAddress());
System.out.println("... remote socket address is " + socket.getRemoteSocketAddress());
output.write(args[1].getBytes());
socket.shutdownOutput();
while (true) {
b = input.read();
if (b == -1) {
break;
}
System.out.print((char) b);
}
System.out.println();
socket.close();
}
}
echoserver.java:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(7000);
System.out.println("The ITEC 6120/8120 Echo Server is now ready!");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Accepted an echo request");
System.out.println("... local socket address " + socket.getLocalSocketAddress());
System.out.println("... remote socket address " + socket.getRemoteSocketAddress());
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
while (true) {
int b = input.read();
if (b == -1) break;
output.write(b);
}
socket.close();
}
}
}
Explanation / Answer
server programme:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int welcomeSocket, newSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if(listen(welcomeSocket,5)==0)
printf("Listening ");
else
printf("Error ");
addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
strcpy(buffer,"Hello World ");
send(newSocket,buffer,13,0);
return 0;
}
client programme:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
printf("Data received: %s",buffer);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.