I am new to Socket Programming. I know this is simple question but I want to learn this.
I am trying with simple example sending a message between client and server. But I am getting error and I couldn't figure what is the problem. Can Some one please help.
How can I get the output (if I run the app.java alone) as
From Process 6
Message sent to client is 12
From Server 12
App.java---------------------->
import java.io.IOException;
public class App {
public static void main(String args[]) throws IOException{
try {
Master master_objec = new Master();
master_objec.start();
Process process_object = new Process();
process_object.start();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Master.java-------------------->
public class Master {
ServerSocket master_socket = null;
private static Socket master_listen = null;
final int PORT_NUMBER = 10004;
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
public void start() throws IOException
{
try
{
master_socket = new ServerSocket(PORT_NUMBER);
System.out.println("Server started. Awaiting connection requests...");
while(true){
//Input Stream
master_listen = master_socket.accept();
InputStream is = master_listen.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
int process_time = br.read();
System.out.println("From Process "+ process_time);
//Out put stream
OutputStream os = master_listen.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
int new_process_time = process_time*2;
bw.write(new_process_time);
System.out.println("Message sent to the client is "+new_process_time);
bw.flush();
}
}catch(IOException ioexception)
{
System.out.println("Error");
}
finally
{
try
{
master_listen.close();
}
catch(Exception e){}
}
}
}
Process.java------------------------------------>
public class Process {
private static Socket connects = null;
int process_time =6;
public void start() throws IOException{
int port = 10004;
connects = new Socket("localhost", port);
OutputStream os = connects.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(process_time);
System.out.println("Message sent to the Server is "+process_time);
bw.flush();
InputStream is = connects.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
int process_time_new = br.read();
System.out.println("From Master "+ process_time_new);
}
}
This is the output I am getiing:
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 Master.start(Master.java:28)
at App.main(App.java:12)
Message sent to the Server is 6
From Master 6