Hi coders!
Im new to Network programming and m currently exploring java.net ...... i am building a simple client server comm.. for that my code for server is:
import java.net.*; import java.io.*; public class Socket { public static void main(String args[]) throws IOException { // Register service on port 1254 ServerSocket s = new ServerSocket(1254); java.net.Socket s1=s.accept(); // Wait and accept a connection // Get a communication stream associated with the socket OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out); // Send a string! dos.writeUTF("Hi there"); // Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); } public InputStream getInputStream() { // TODO Auto-generated method stub return null; } }
and for client is:
import java.net.*; import java.io.*; public class Client { public static void main(String[] args) { // Open your connection to a server, at port 1254 Socket s1 = new Socket("localhost",1254); // Get an input file handle from the socket and read the input InputStream s1In = s1.getInputStream(); DataInputStream dis = new DataInputStream(s1In); String st = new String (dis.readUTF()); System.out.println(st); // When done, just close the connection and exit dis.close(); s1In.close(); s1.close(); } }
the error returned is:
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unkno wn Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at Socket.main(Socket.java:9)
i need to know that y this so...moreover how should i manage to run both programs on same machine to get client-server comm....
Any help will be appreciated.
thanks in advance