我必须创建一个由服务器和 4 个客户端组成的分布式系统,服务器有一个文件,所有客户端不能同时读取,而只有一个可以写入。每个客户端随机选择是读取文件的内容还是写入文件的内容。
如果客户端选择写入,它会生成一个介于 0 和 911 之间的随机数,该随机数会写入到服务器提供给客户端的文件中。当随机数为 100 时,客户端与服务器断开连接。您必须实施适当的机制以使服务器能够同步客户端。
这是错误:
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)
这是代码:
服务器
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();
}
}
}