サーバーと 4 つのクライアントで構成される分散システムを作成する必要があります。サーバーには、すべてのクライアントが同時に読み取ることができないファイルがあり、書き込みは 1 つだけです。各クライアントは、ファイルの内容を読み取るか書き込むかをランダムに選択します。
クライアントが書き込みを選択した場合、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();
}
}
}