Socket Programming 1

import java.io.*;
import java.net.*;
public class client1{
public static void main(String args[]){

try{
//BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket=new Socket("127.0.0.1",1574);
System.out.println("connected");
InputStreamReader stream=new InputStreamReader(clientSocket.getInputStream());
DataOutputStream os=new DataOutputStream(clientSocket.getOutputStream());
BufferedReader reader=new BufferedReader(stream);
if(reader!=null){
System.out.println("reader is not null");
}
String message=reader.readLine();
System.out.println("received from server " +message);
os.writeBytes("Hello\n");
clientSocket.close();
}catch(IOException e){
System.out.println(e);
}
}
}




import java.io.*;
import java.net.*;

public class server1{
public static void main(String args[]){
String clientSentence;

try{
ServerSocket sock=new ServerSocket(1574);
System.out.println("waiting...");

while(true){
Socket connSoc=sock.accept();
System.out.println("connection accepted:");
InputStreamReader is=new InputStreamReader(connSoc.getInputStream());
BufferedReader br=new BufferedReader(is);
//OutputStream os=connSoc.getOutputStream();
//System.out.println("message received from client" +os);
String s=br.readLine();
System.out.println("message from client " +s);
PrintWriter writer=new PrintWriter(connSoc.getOutputStream());
writer.println("hi");
String msg=br.readLine();
System.out.println("message from client" +msg);
writer.close();
connSoc.close();
}

}catch(IOException e){
System.out.println(e);
}
}
}

Post a Comment

أحدث أقدم