0

How to communicate between different computers using socket???

10th Dec 2016, 5:15 PM
shravan
shravan - avatar
3 Respuestas
0
you need to make a server then created a client which has to connect to the same server IP / port.
10th Dec 2016, 5:35 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
0
I'll give you my code for a server and a client
10th Dec 2016, 5:35 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
0
Server: import java.io.*; import java.net.*; class Main{ public static void main(String[] args) throws IOException{ try{ ServerSocket ss = new ServerSocket(1201); Socket s = ss.accept(); DataInputStream din = new DataInputStream(s.getInputStream()); DataOutputStream dout = new DataOutputStream(s.getOutputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String msgin = "", msGout = ""; while(!msgin.equals("end")){ msgin = din.readUTF(); System.out.println(msgin); msGout = br.readLine(); dout.writeUTF(msGout); } s.close(); }catch(Exception e){ //handle excpetions } } } Client: import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; class Main{ public static void main(String[] args) throws IOException{ try{ Socket s = new Socket("127.0.0.1", 1201); DataInputStream din = new DataInputStream(s.getInputStream()); DataOutputStream dout = new DataOutputStream(s.getOutputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String msgin = "", msgout = "";"" while(!msgin.equals("end")){ msgout = br.readLine(); dout.writeUTF(msgout); msgin = din.readUTF(); System.out.println(msgin); } }catch (Exception e){ } } }
10th Dec 2016, 5:37 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar