Note: I have added main method in the P2PUtil class to test the code. Also, I have provided the sever program also.
Source code:
Client:
import java.io.*;
import java.net.Socket;
/**
* This class holds any utility methods needed for P2P networking communication.
*/
public class P2PUtil {
/**
* Allows a one time socket call to a server, gets reply, and then closes connection.
* @param sIP
* @param iPort
* @param sMessage
* @return
*/
public static String connectForOneMessage(String
sIP, int iPort, String sMessage) {
String rMessage = null;
try{
//Connecting to
the server
System.out.println("Connecting to " + sIP + " on port " +
iPort);
Socket client = new Socket(sIP, iPort);
//connection established
System.out.println("Connected to server " +
client.getRemoteSocketAddress());
//Getting output stream
OutputStream outToServer =
client.getOutputStream();
DataOutputStream out = new
DataOutputStream(outToServer);
//sending message to the server
out.writeUTF(sMessage);
//getting input stream
InputStream inFromServer =
client.getInputStream();
DataInputStream in = new
DataInputStream(inFromServer);
//receiving message from server
rMessage = in.readUTF();
//closing the connection
client.close();
}
catch (IOException e) {
e.printStackTrace();
}
return rMessage;
}
public static void main(String [] args){
String sIP =
"192.168.100.8"; //server IP address
int sPort = 200;
//server port address
String sMessage = "Hello, I am your
client. Please say hello to me."; //message to the
server
//connecting to server
String retMessage =
connectForOneMessage(sIP, sPort, sMessage);
//printing return message from
server
System.out.println("Server Replied:
" + retMessage);
}
}
Server:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class P2PServer {
public static void main(String[] args){
try {
@SuppressWarnings("resource")
//creating
server socket object
ServerSocket
serverSocket = new ServerSocket(200);
//waiting for a
client connection
System.out.println("Waiting for a client request on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
//connected to aclient
System.out.println("Connected to the client" +
server.getRemoteSocketAddress());
//input stream object
DataInputStream in = new
DataInputStream(server.getInputStream());
//printing message from the client
System.out.println("Message from the client: " +
in.readUTF());
//output stream object
DataOutputStream out = new
DataOutputStream(server.getOutputStream());
//sending message to the client
out.writeUTF("Hello. Thank you for connecting.");
//closing the connection
server.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Code screenshot:
client:


server:


Output:
Client side:

Server side:

h. P2PUtil i. connectForOneMessage: 1. This method will connect to a given server via Socket for...
Modify the socket-based date server below so that the server services each client request in a separate thread. import java.net.*; import java.io.*; public class DateClient { public static void main(String[] args) { try { /* make connection to server socket */ Socket sock = new Socket(“127.0.0.1”,6013); InputStream in = sock.getInputStream(); BufferedReader bin = new BufferedReader(new InputStreamReader(in)); /* read the date from the socket */ String line; while ( (line = bin.readLine()) != null) System.out.println(line); /* close the socket connection*/ sock.close(); ...
I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...
Q7 The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks...
The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks with...
Error: Main method not found in class ServiceProvider, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application This is the error im getting while executing the following code Can you modify the error import java.net.*; import java.io.*; public class ServiceProvider extends Thread { //initialize socket and input stream private Socket socket = null; private ServerSocket server = null; private DataInputStream in = null; private DataOutputStream out = null; private int...
Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...
I have to modify a server program and chat program to work as the following instructions but I am completely clueless as to where to start. I'd appreciate any help on how to atleast get started. This must be done in java. Diffie-Hellman Two parties use a key agreement protocol to generate identical secret keys for encryption without ever having to transmit the secret key. The protocol works by both parties agreeing on a set of values (a) and (q)....
I need help with this assignment, please; Programming Assignment 3: UDP Pinger Lab In this lab, you will study a simple Internet ping server written in the Java language, and implement a corresponding client. The functionality provided by these programs is similar to the standard ping programs available in modern operating systems, except that they use UDP rather than Internet Control Message Protocol (ICMP) to communicate with each other. (Java does not provide a straightforward means to interact with ICMP.)...
1. Create two directories from a DOS or Unix prompt. Name one directory 'RMILabServer' and the other directory 'RMILabClient'. 2. Within the server directory, save the following three classes. //************************************************************ // Calculator.java Interface for a Calculator import java.rmi.*; public interface Calculator extends Remote { // this method will be called from remote clients int add (int x, int y) throws RemoteException; } //************************************************************* // CalculatorServant.java // A Remote object class that implements Calculator. import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class CalculatorServant...