the code snippet i'm trying to execute
ServerSocket ss=new ServerSocket(7777);
Socket socket=ss.accept();
System.out.println("Connection:"+socket.isConnected());
i'm trying to get data from another program to this class i'm using same port for Socket,ServerSocket.
another program code
Socket socket=new Socket("localhost",7777);
fileData.append(LineNumber + ": " + inputLine + "\n");//fileData is jtext area
DataOutputStream dos4=new DataOutputStream(socket.getOutputStream());
dos4.writeUTF(LineNumber + ": " + inputLine + "\n");
dos4.close();
socket.close();
If you want to communicate through socket here one example which will guide you about socket programming
it will listen to client
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=dis.readUTF();
System.out.println("message= "+str);
dis.close();
Here is client code who will write to server
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.