我需要创建一个程序,其中对等方需要与其他对等方通信。我模糊地创建了一个结构,但它不起作用。我知道我犯了一个错误,但无法弄清楚问题。我创建一个服务器套接字作为一个线程,并创建一个客户端套接字线程,它将与服务器套接字线程通信。进入港口后,程序什么都不做......
server.java
------------
class server
{
public static void main(String argv[]) throws Exception
{
//server socket
ServerSocket server = new ServerSocket(1001);
Socket client;
while(true)
{
client = server.accept();
Thread t = new Thread(new acceptconnection(client));
t.start();
}
}
}
class acceptconnection implements Runnable
{
BufferedReader inFromClient,inn;
DataOutputStream ds;
Socket clientsocket;
acceptconnection (Socket socket) throws IOException
{
this.clientsocket = socket;
inn = new BufferedReader (new InputStreamReader(System.in));
inFromClient =new BufferedReader(new
InputStreamReader(clientsocket.getInputStream()));
ds = new DataOutputStream(clientsocket.getOutputStream());
}
@Override
public void run ()
{
try
{
String clientSentence, inp;
while(( clientSentence = inFromClient.readLine())!=null)
{
System.out.println("message from remote socket #" +
clientsocket.getRemoteSocketAddress()+ clientSentence);
inp=inn.readLine();
ds.writeBytes(inp + "\n");
ds.flush();
}
}
}
}
client.java
------------
class client
{
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
Socket clientSocket = null;
// ss = new ServerSocket(10002);
clientSocket = new Socket("localhost", 1001);
BufferedReader inFromUser = new BufferedReader( new
InputStreamReader(System.in));
DataOutputStream outToServer = new
DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
System.out.println("Enter you name:");
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + "\n" );
System.out.println("Enter the port u want:");
sentence = inFromUser.readLine();
int serverport = Integer.parseInt(sentence);
Thread t = new Thread(new acceptconnection1(serverport));
t.start();
String msg;
clientSocket.close();
}
}
class acceptconnection1 implements Runnable {
BufferedReader inserver, inn;
DataOutputStream ds;
Socket socket, peersocket;
int serverport ;
Socket clientSocket = null;
acceptconnection1 (int serverport) throws IOException{
this.serverport = serverport;}
public void run () {
ServerSocket ss;
String cs,a;
try {
ss = new ServerSocket(serverport);
while(true)
{
peersocket =ss.accept();
Thread t = new Thread(new abc(peersocket) );
t.start();
}}}
class abc implements Runnable {
BufferedReader inn,inp;
DataOutputStream ds;
Socket peersocket;
public abc(Socket peersocket) throws IOException{
this.peersocket = peersocket;
inn = new BufferedReader (new InputStreamReader(System.in));
inp =new BufferedReader(new InputStreamReader(peersocket.getInputStream()));
ds = new DataOutputStream(peersocket.getOutputStream());
}
@Override
public void run()
{
String clientSentence;
Socket client = new Socket();
try {
while(( clientSentence = inp.readLine())!=null)
{
ds.writeBytes("wake up!");
//System.out.println("message=" + clientSentence);
}