I want to make a server and client program and transfer variables between they. I tried for a server program :
package testSocket;
import java.net.*;
import java.io.*;
import javax.swing.JFrame;
public class server {
public static void main(String[] args) throws IOException {
JFrame jframe = new JFrame("TEST SEVER");
jframe.setVisible(true);
jframe.setSize(800, 600);
try (ServerSocket sws = new ServerSocket(12480)) {
Socket socket = sws.accept();
OutputStream outs = socket.getOutputStream();
DataOutputStream douts = new DataOutputStream(outs);
douts.writeUTF("Hello.");
douts.close();
outs.close();
} catch (Exception e) {
System.out.println("Exception : " + e);
}
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And I tried for a client program :
package testSocket;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class client {
public static void main(String[] args) throws IOException {
Socket tcs = new Socket("localhost", 12480);
InputStream iss = tcs.getInputStream();
DataInputStream dis = new DataInputStream(iss);
String str = new String(dis.readUTF());
JOptionPane.showMessageDialog(null, "test" , str , JOptionPane.YES_NO_CANCEL_OPTION);
dis.close();
iss.close();
tcs.close();
}
}
But the program did not work and gave the error :
Exception : java.net.BindException: Address already in use: NET_Bind
How can I solve this error ? Can you recommend anything ?
Thanks.