Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

10:40 AM 100%, O Ooredoo elearning.qu.edu.qa Submission due: March 26 2017 midni

ID: 3804208 • Letter: 1

Question

10:40 AM 100%, O Ooredoo elearning.qu.edu.qa Submission due: March 26 2017 midnight 1 of 3 Assignment submission Provide your report with a cover page containing your name, student number, and e-mail Include code test, and attach all output text and non-text files, if any, including reenshots, logs, etc. nclude a results with clear comments on each result into the report. Submit all assignment materials in one archive file "Assignment 1Hstudent nameHstudent no) zip" to blackboard by the due date shown above. Assignment description Introduction The objectives of this assignment are as follows: 1- To exercise your development skills, including socket programming and multi- threading to build a small system that demonstrates a working chat program. 2- To get familiar with the peer-to-peer application model designing application that works as both client and server without having centralized server. CompMer engineering program-Spring 2017 Tasks write an application using Java or C min to implement a simple command line peer-to-peer ch system through socket programming, and multi-threading using the at following specifications. The application is implemented using a module named "p2pcha 2. The application uses TCP as the transport protocol. The p2pchat module works as both client and server using multithreading to allow for sendin and recei e same time. 4. When the p2pchat module starts using the command "p2pchat" ie. without command- line parameters, it listens on port 5789 for incoming connections. If it receives a connection request, it accepts the connection, and opens a (separate sending thread) to wait for the user input to send to the other peer-to-peer chat program, while listens to the ncoming text on he main program thread. 5. If the module starts using the command "p2pchat lip-address it initiates a connection with another p2pchat with the IP address given in the lip-addressl command-line parameter. It then opens a (separate sending thread that waits for the user input text to be sent to the other peer-to-peer chat program, while listens to the incoming text en the main program thread. 6. Text chat when the user writes on-command text (ie, any text that does not start with the word "sendfile" on any p2pchat peer and presses enter, the text message appears on the other p2pchat peer window. Send the content of a small text file: If the user writes a text that starts with sendfile filename and location the sending thread will read the content of the file highlighted by the filename and location d send the content of the file to the other peer-to-peer chat program. 8. Developing a graphical user interface for the program is optional, provided that all the specifications above are fulfilled.

Explanation / Answer

for server side program..........

import java .io.*;

import java .net.ServerSocket;

import java .net.SocketException;

  

public class chatSocketServer{

{

private ServerSocket ServerSocket=null;

private Socket socket =null

private InputStream inStream=null;

private OutputStream=null;

public chatSocketServer()

{

}

public void CreateSocket()

{

try{

ServerSocket serversocket=new serversocket(3339);

while(true)

{

Socket =serverSocket.accept();

instream=Socket.getInputStream();

outStream=Socket.getOutStream();

System.out.println("Connected");

CreateReadThread();

createWriteThread();

}}}

}

catch(IOException io)

{

io.printStackTrace();

}

}

public void createReadThread()

{

Thraed readThread=new Thread()

{

public void run()

{

while(socket.isConnected())

{

try{

byte[] readBuffer=new byte[200];

int num=instream.read(readBuffer);

if(num>0)

{

byte[]]=arrayByte=new byte[num];

System.arraycopy(readBuffer,0,arrayBytes,0,num);

String recevedmsg=new String(arrayBytes,"UTF-8");

system.out.println("msg receved"+recevedmsg);

}}

else

notify();

}

;

}catch(SocketException se)

{

System.exit(0);

}

catch(IOExceptipon )

{

i.printStackTrace();

}

}

};

readThread.setPriority(Thread.MAX_PRIORITY);

readThread.start();

}

public void createwriteThread()

{

ThreadWrtiteThread=new Thread()

{

public void run()

{

while(Socket.isConnected())

{

try

{

BufferedReader inputReader=new BufferedReader(new inputStreadReader(system.in));

sleep(100);

String typedmsg=inputReader.readLine();

if(typedmsg!=null && typedmsg.length()>0;

{

Synchronized(socket)

{

outStream.write(typedmsg.getBytes("UTF-8));

sleep(100);

}

}

notify();

};

}

catch(IOException}

{

}

catch(Interrupted Exception ie)

{

ie.printStackTrace();

}

}

}

};

WriteThread.setPriority(Thread.Max_PRIORITY);

writeThread.start();

}}

public ststic void main(String[] arg)

{

chatSocketServer chatServer=new chatSocketServer();

chatServer.createSocket();

}

}

do lyk this socket program for client side also

import waht are there in above

then constructor

then

public void CreateSocket()

{

try{

Socket=new Socket("localHost",3339)

System.out.println(""Connected");

inStream=Socket.getInputStream();

outStream=Socket.getOutStream();

createreadThread();

createWriteThread();

}

catch(UnkownHostException u)

}

catch(IOException io)

}

Then u create readThread

for that set priority same as the server side program

then write Thread same lyk server side.

then

public static void main(string[] args)throws Exception

{

ChatSocketClient mychatClient=new ChatSocketClient();

myChatClient.createSocket();

myChatClient.createWriterThrea();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote