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

code in c programming language Write two socket programs and name one sender and

ID: 3704310 • Letter: C

Question

code in c programming language

Write two socket programs and name one sender and other one receiver. Receiver: will open socket (TCP) on port specified on command line ( typical use socket grater than 2000 for testing). Establish any connection request and echo all information that it reads from the socket to standard output (terminal) until it see "ZZ" as part of the communication. As soon as it see ZZ, display all information up to ZZ and exit the program. Sender: Will have two command line options, first an IP address and second a port number. Sender will open socket connection on port specified on command line to send information to receiver (Receiver should listen on the same port number). Sender will request user input from standard input (keyboard) and send out the information as soon as user press enter key, program will continue to read use I until it reads "ZZ" where at this point, it can send all unsent data if any and close the connection with Receiver program Submit: You may develop your program any platform, but must use sockets. You final program should be compile on joker. 9

Explanation / Answer

///sender (client)

import java.io.*;
import java.awt.*;
import java.awt.color.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;

import java.io.*;
import java.awt.*;
import java.awt.color.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;

public class main implements Runnable

{

public static void main (String[] args)

{

main s=new main();

}

public void run()

{

boolean done=true;

while(done)

{

//System.out.println(" Thread in Run +read ");

try

{

if(dothis)

{

String str =br.readLine();

//System.out.println("str="+str);

getFirstElement(str);

if(str==null)

main.s.setKeepAlive(false);

}

}

catch(Exception e)

{  

System.out.println(" Thread in Run Exception ");

}

}//wh

}//run

public void connection(String IPAddress,String portno )

{  

int pno=Integer.parseInt(portno);

try

{

s=new Socket(IPAddress,pno);  

}

catch(Exception ee)

{

}

try

{

System.out.println("connection is "+s.isConnected());

br = new BufferedReader(new InputStreamReader(s.getInputStream()));   

bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

Thread th;

th = new Thread(this);

th.start();

}

catch(Exception ue)

{

System.out.println(" Connection not performed");

}

}

}

..................................................................................................................................................................................................

//receiver (server)

import java.io.*;
import java.awt.*;
import java.awt.color.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;

public class main implement Runnable

{

public static void main (String[] args)

{

main s=new main();

}

public void run()

{

System.out.println("run");

boolean done=true;

while(done)

{

System.out.println("while ");

try

{

if(dothis)

{

String str =br.readLine();

System.out.println(str);

if(str!=null)

getFirstElement(str);

if(str==null)

{

main.s.close();  

}

}  

}

catch(Exception e)

{  

}

}//wh

}//run

public void connection(int portno )

{  

System.out.println("in connection"+portno );

try

{

ss=new ServerSocket(portno);

s=ss.accept();

}

catch(Exception ee)

{

}

try

{

System.out.println("connection is "+s.isConnected());

br = new BufferedReader(new InputStreamReader(s.getInputStream()));   

bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

Thread th;

th = new Thread(this);

th.start();

}

catch(Exception ue)

{

System.out.println(" Connection not performed");

}

}

}

}