Question

Using the programming language of your choice, write a client application which: Connects to a network...

Using the programming language of your choice, write a client application which: Connects to a network service, then Asks for version information, then Receives the response, then Disconnects and Prints out the response.

Detail:

The program will take no input from the user.

When your program is executed, it should connect to IP address 64.183.98.170 on port 3800. Upon successful connection, send the string:

version\n

where \n is an end of line marker.

The server will respond with a string and then disconnect. Your program should then also disconnect and print the result.

The output of your program should be the IP address and port number of the service you connected to with the version information the server responded with.

 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Server.java

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream incommingStream = null;
private String version = "";
private DataOutputStream outgoingtStream = null;
  
public Server(int portNumber)
{
// server starts: waits for client connection
try
{
server = new ServerSocket(portNumber);
System.out.println("Server: Service initiated at port: " + portNumber + "\nServer: Waiting for client connection...\n");
  
// accepts the incomming client connection
socket = server.accept();
System.out.println("Server: Connected with the client!\n");
  
// creating a channel to accept the client request string
incommingStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
  
// reading the client request
String clientLine = incommingStream.readUTF();
System.out.println("Server: Client communicated with " + clientLine + " request...");

// if the client string is "version"
if (clientLine.contains("version"))
{
version = System.getProperty("os.name") + ", Version " + System.getProperty("os.version");
}
else
{
version = "Unrecognized request!";
}
  
// creating a channel to send the response to the client
outgoingtStream = new DataOutputStream(socket.getOutputStream());
  
// writes the response string over the channel
outgoingtStream.writeUTF(version);
  
System.out.println("Server: Disconnected!");

//closing connection
socket.close();
incommingStream.close();
outgoingtStream.close();
}catch(IOException ex){
System.out.println("Server error: " + ex.getMessage());
}
}
  
public static void main(String[] args)
{
Server server = new Server(3800);
}
}

Client.java

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
  
private Socket socket = null;
private DataInputStream incommingStream = null;
private DataOutputStream outgoingStream = null;
private String serverResponse = "";
  
public Client(String ipAddress, int portNumber)
{
// trying to establish a connection to the server through the provided IP Address and Port Number
try
{
socket = new Socket(ipAddress, portNumber);
System.out.println("Client: Connection established with the server!");
  
// creating a channel to send something to the server
outgoingStream = new DataOutputStream(socket.getOutputStream());
  
// the client request string
String clientString = "version\n";
System.out.println("Client: Sending query to the server...\n");
  
// sending the "version" request to the server
outgoingStream.writeUTF(clientString);
System.out.println("Client: sending completed!");
  
System.out.println("Client: Waiting for server response...\n");
  
// receives the server response over the input channel
incommingStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
  
// reading the server response
serverResponse = incommingStream.readUTF();
System.out.println("Client: Server responded with version: " + serverResponse);
  
System.out.println("Client: Disconnected!");
  
// closing connection
outgoingStream.close();
socket.close();
  
}catch(UnknownHostException ec){
System.out.println("Unknown host error: " + ec.getMessage());
}catch(IOException ex){
System.out.println("Connection error: " + ex.getMessage());
}
}
  
public static void main(String[] args)
{
Client client = new Client("127.0.0.1", 3800);
}
}

Note: First run the "Server.java" file and then run the "Client.java" file!

Add a comment
Know the answer?
Add Answer to:
Using the programming language of your choice, write a client application which: Connects to a network...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using network sockets, write a C program called client that receives three command-line arguments in the...

    Using network sockets, write a C program called client that receives three command-line arguments in the form: client host port file and sends a request to a web server. The command-line arguments are hostRepresents the web server to connect to port Represents the port number where a request is sent. Normally an HTTP request is sent over port 80, but this format allows for custom ports file Represents the file requested from the web server Your program should create a...

  • Using python 3 to create the UDP Ping Clien and server. Using UDP sockets, you will...

    Using python 3 to create the UDP Ping Clien and server. Using UDP sockets, you will write a client and server program that enables the client to determine the round-trip time (RTT) to the server. To determine the RTT delay, the client records the time on sending a ping request to the server, and then records the time on receiving a ping response from the server. The difference in the two times is the RTT. The ping message contains 2...

  • Assignment One program will be the update server and the other will be the update client....

    Assignment One program will be the update server and the other will be the update client. Here is how the two programs should interact with each other: Server 1. the server starts up and reads the version number in the data.bin file and stores it in memory. 2. The server binds to a port and awaits connections. Client 1. The client starts up and it will first read the version number found within it's own data.bin file. 2. The client...

  • To develop a client/server application using TCP sockets and the C programming language that is capable...

    To develop a client/server application using TCP sockets and the C programming language that is capable of supporting multiple concurrent service requests from different clients. PROBLEM You are to use the Ubuntu operating system as well as both the client and the server programs. You are to modify your server program to process requests from more than one client concurrently. This means different clients may request either the same service or a total different one. The services supported by your...

  • Write two programs Client and Server

    Given two integer matrices A and B, you are requested to compose a program to perform matrix addition (A + B). Both matrices have N rows and M columns; N > 1, M > 1; You need to divide both (A and B) into four equal (or close to equal) size of submatrices (A0,0, A0,1, A1,0, A1,1 and B0,0, B0,1, B1.0, B1.1)andeachsubmatrixhasdimensioncloseto(N/2)x(M/2). YouneedtocreatefourJavathreads each thread performs a subset of addition on one pair of the submatrices. Example, thread 0 performs addition...

  • implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of...

    implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of the classes exatcly the same as requested for testing purpose. please follow each instruction provided below Objective of this assignment o get you famililar with developing and implementing TCP or UDP sockets. What you need to do: I. Implement a simple TCP Client-Server application 2. and analyze round trip time measurements for each of the above applications 3. The...

  • In this assignment, you design a simple chat room in the form of a network application which uses the services of a TCP/IP computer network. Your design should have a clientserver architecture in whic...

    In this assignment, you design a simple chat room in the form of a network application which uses the services of a TCP/IP computer network. Your design should have a clientserver architecture in which the server is multi-threaded. Then, you need to implement the server-side of the chat-room application in Java (implementing the client-side is optional). The server maintains a list (an ArrayList will work well) of all the active connections. It will listen on a port for a new...

  • For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will...

    For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will modifiy echoServer.c and echoClient.c programs (for the server's port# not fixed). (1) Modify echoServer.c program to take one argument (a port number) to be used for its listening port when it starts. (2) Modify echoClient.c program to take two arguments (server's IP address and Port number) to be used for its connection. (3) Find a port free for the server using netstat (see...

  • 166 Chapter 8: TCP/IP Applications Getting Down to Business The way network communication all those ls...

    166 Chapter 8: TCP/IP Applications Getting Down to Business The way network communication all those ls and Os) goes in and out of a machine physically is through the NIC (network interface card). The way network communication goes in and out of a machine logically though, is through a program or service. A service is a program that runs in the background, independent of a logon, that provides functionalities to a system. Windows client machines, for instance, have a Workstation...

  • Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array...

    Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array of struct student_info ( char abc123171 char name [101 double GPA; To simplify the tasks, the server should create a static array of 10 students with random abc123, name, and GPA in the server program. Then the server waits for clients. When a client...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT