Reputation: 161
I have to create a distributed system that consists of a server and 4 clients, The server has a file that all client can not read simultaneously, while only one to write in it. Each client chooses randomly whether to read the contents of the file or whether to write to it.
If the client elects to write, it generates a random number between 0 and 911, which is written in the file given to the client the server. A client disconnects from the server when the random number is 100. You must implement appropriate mechanisms to enable the server to synchronize clients.
Here's the error:
Error during I/O
Error during I/O java.net.BindException: Address already in use:
JVM_Bind at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unknown 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
My_Thread.run(My_Thread.java:18) java.net.BindException: Address
already in use: JVM_Bind at
java.net.DualStackPlainSocketImpl.bind0(Native Method) at
java.net.DualStackPlainSocketImpl.socketBind(Unknown 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
My_Thread.run(My_Thread.java:18) Waiting Incoming Connection... Local
Address :0.0.0.0/0.0.0.0 Port :5555 Error during I/O
java.net.SocketException: Connection reset at
java.net.SocketInputStream.read(Unknown Source) at
java.net.SocketInputStream.read(Unknown Source) at
sun.nio.cs.StreamDecoder.readBytes(Unknown Source) at
sun.nio.cs.StreamDecoder.implRead(Unknown Source) at
sun.nio.cs.StreamDecoder.read(Unknown Source) at
java.io.InputStreamReader.read(Unknown Source) at
java.io.BufferedReader.fill(Unknown Source) at
java.io.BufferedReader.readLine(Unknown Source) at
java.io.BufferedReader.readLine(Unknown Source) at
Server.main(Server.java:34)
Here's the code:
server
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class Server {
public static void main(String[] args) {
My_Thread t1=new My_Thread();
My_Thread t2=new My_Thread();
t1.start();
t2.start();
try{
ServerSocket server = new ServerSocket(5555,50);
System.out.println("Waiting Incoming Connection...");
System.out.println("Local Address :"+server.getInetAddress()+" Port :"+server.getLocalPort());
Socket sock = server.accept();
BufferedReader instream = new BufferedReader (new InputStreamReader (sock.getInputStream()));
BufferedWriter outstream = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
FileWriter file = null;
int number=0;
try
{
file = new FileWriter("C://Users//Vagos//Desktop//file.txt");
}
catch(IOException ex)
{
System.out.println("File Error");
}
Scanner scanner = new Scanner(new File("file.txt"));
String strin = instream.readLine(); <---and heare
if (strin.equals("Insert")){ //following the protocol
do{
//strin = instream.readLine();
//outstream.write(strin+"\n");
//outstream.flush();
number = instream.read();
file.write(new Integer(number).toString());
file.write(" ");
file.flush();
}while(number!=100); //bye = terminate the conversation
}else if(strin.equals("Read") ){
while(scanner.hasNextInt()){
outstream.write(scanner.nextInt());
outstream.flush();
}
}else {
System.out.println("Connection Closing...");
}
instream.close();
outstream.close();
file.close();
sock.close();
}catch (Exception ex){
System.out.println("Error during I/O");
ex.getMessage();
ex.printStackTrace();
}}}
client
import java.net.*;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class client {
public static void main(String[] args) {
try{
Socket sock = new Socket("localhost", 5555);
BufferedReader instream = new BufferedReader (new InputStreamReader(sock.getInputStream()));
BufferedWriter outstream = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
System.out.println("Sending Messages to the Server...");
System.out.println("Connecting to "+ sock.getInetAddress()+ " and port "+sock.getPort());
System.out.println("Local Address :"+sock.getLocalAddress()+" Port :"+sock.getLocalPort());
Random random = new Random(System.currentTimeMillis());
String strin, strout;
int number=0,choice=0;;
choice=random.nextInt(2);
System.out.println("choice "+choice);
if(choice==1){
outstream.write("Insert\n");
outstream.flush();
do{
number=random.nextInt(911);
System.out.print(number);
outstream.write(number);
outstream.flush();
}while (number!=100);
}else if(choice==2){
outstream.write("Read\n");
outstream.flush();
do{
number=instream.read();
System.out.println(number);
}while (true);
}
}catch (Exception ex){
System.out.println("Connection Refused!!!");
}
}
}
My_Thread
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class My_Thread extends Thread{
public void run(){
try{
ServerSocket server = new ServerSocket(5555,50); <---heare
System.out.println("Waiting Incoming Connection...");
System.out.println("Local Address :"+server.getInetAddress()+" Port :"+server.getLocalPort());
Socket sock = server.accept();
BufferedReader instream = new BufferedReader (new InputStreamReader (sock.getInputStream()));
BufferedWriter outstream = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
FileWriter file = null;
int number=0;
try
{
file = new FileWriter("C://Users//Vagos//Desktop//file.txt");
}
catch(IOException ex)
{
System.out.println("File Error");
}
Scanner scanner = new Scanner(new File("file.txt"));
String strin = instream.readLine();
if (strin.equals("Insert")){ //following the protocol
do{
//strin = instream.readLine();
//outstream.write(strin+"\n");
//outstream.flush();
number = instream.read();
file.write(new Integer(number).toString());
file.write(" ");
file.flush();
}while(number!=100); //bye = terminate the conversation
}else if(strin.equals("Read") ){
while(scanner.hasNextInt()){
outstream.write(scanner.nextInt());
outstream.flush();
}
}else {
System.out.println("Connection Closing...");
}
instream.close();
outstream.close();
file.close();
sock.close();
}catch (Exception ex){
System.out.println("Error during I/O");
ex.getMessage();
ex.printStackTrace();
}
}
}
Upvotes: 1
Views: 2599
Reputation: 80623
You can only bind to a port once from your program (unless you set a specific socket option, SO_REUSEADDRESS
, but ignore that for now). As it is currently, you are attempting to bind a process to the same port three times - once in your for loop, and one time each for the two threads you start up. I'm not entirely sure why you are starting up three server processes, but you will want to revisit your design and make sure you are only binding the server port once.
Edit:
What I would do is remove the try/catch logic from the main method, and spawn just one thread:
public static void main(String[] args) {
My_Thread t1 = new My_Thread();
t1.start();
}
Upvotes: 2