Socket Programming 7

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

public class Server{
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:");

PrintWriter writer=new PrintWriter(connSoc.getOutputStream());
writer.println("hi");
writer.close();
connSoc.close();
}

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





import java.io.*;
import java.net.*;
public class client{
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());
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);
clientSocket.close();
}catch(IOException e){
System.out.println(e);
}
}
}

Post a Comment

أحدث أقدم