Client Side
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
//this program just sent a message to the server through a socket("localhost",3009);
public class Clientsent {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Socket requestSocket;
DataOutputStream out;
DataInputStream in;
String sent="how was the day";
try{
requestSocket = new Socket("localhost", 3009);
System.out.println("Connected to the server ");
out = new DataOutputStream(requestSocket.getOutputStream());//creating outputstream object (for writing data to a socket("local",3009) so that the server can read that data from the that socket)
out.flush();
out.writeUTF(sent) ; //method that writes the message to the socket("localhost",3009);
System.out.println(sent);
in = new DataInputStream(requestSocket.getInputStream()); //creating inputstream object for reading data from socket from socket("localhost",3009).
String getmessagefromserver= in.readUTF();// method that reads data from the socket....
System.out.println("msg from server >>>>"+getmessagefromserver);
out.flush();
out.close();
in.close();
requestSocket.close();
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public static void main(String[] args) {
ServerSocket providerSocket;
Socket skt;
DataOutputStream out;
DataInputStream in;
try{
providerSocket = new ServerSocket(3009);
System.out.println("Waiting for connection");
System.out.println("Ready to Connect");
skt= providerSocket.accept();//listens for any client requestand assign to a socket obj
in = new DataInputStream(skt.getInputStream());
System.out.println("error locating");
String s;
s=in.readUTF();
System.out.println("error isolating");
System.out.println("client >>"+s);
out = new DataOutputStream(skt.getOutputStream());
out.writeUTF("message accepted");
out.flush();
in.close();
out.close();
skt.close();
}
catch(Exception e){
System.out.println("error"+e);
}
}
}
0 comments:
Post a Comment