我正在尝试创建一个程序,服务器和客户端可以同时向/从彼此发送和接收消息(与两个有电话的人可以互相发送文本的方式相同)
目前我只想关注服务器和主文件三个文件(主要功能文件,服务器文件,客户端文件)。
服务器文件的问题是有两个独立的线程,每个线程都有自己独立的“运行”功能,所以我想知道我应该把“socket.accept()”行放在哪里才能使两个他们的工作(可能之前,全球某种程度上?)
运行服务器的命令行参数是
java DirectMessengerCombined -l 3000
如果“-l”不存在,那么它将作为客户端运行
我认为服务器文件的流程会像这样(psuedo-code comments)(如果我错了,请纠正我)
//Server listens for connections
//then accepts the connection from client
//Recieving msesages:
//function recieves messages, create and run a functon that recieves messages
//read from the socket until the other side closes
//display the recieved message
//Sending: Standard input begins
//create and run a functon that sends messages
//write using standard input as long as the user doesn't close it, in a loop
//user close standard input to end the program
根据这个流程,是否可以从线程接受第一个run方法之外的连接?也许在构造函数中?
服务器代码:
import java.io.*;
import java.net.*;
import java.util.*;
import javax.imageio.IIOException;
public class DirectMessengerServer
{
private String[] serverArgs;
private static Socket socket;
public boolean keepRunning = true;
public DirectMessengerServer(String[] args) throws IOException
{
// should serverSocket.accept() go here???
// set the instance variable
this.serverArgs = args;
int port_number1 = Integer.valueOf(serverArgs[1]);
ServerSocket serverSocket = new ServerSocket(port_number1);
socket = serverSocket.accept();
}
public String[] ServerRun(String[] args)
{
serverArgs = args;
serverArgs = Arrays.copyOf(args, args.length);
return serverArgs;
}
// should serverSocket.accept() go here???
Thread ServerRecieve = new Thread();
//If i put serverSocket.accept() in both the run methods, won't that cause an "Address already in use error"?
//run method of ServerRecieve
public void run(String args[])
{
System.out.println("Server recieve thread is now running");
try
{
while(keepRunning)
{
//Reading the message from the client
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String MessageFromClient = br.readLine();
System.out.println("Message received from client: "+ MessageFromClient);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Thread ServerSend = new Thread ();
//Run method of ServerSend
public void run()
{
while(keepRunning)
{
System.out.println("Server sending thread is now running");
try
{
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
//creating message to send from standard input
String newmessage = "";
try
{
// input the message from standard input
BufferedReader input= new BufferedReader(
new InputStreamReader(System.in));
String line = "";
line= input.readLine();
newmessage += line + " ";
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
}
String sendMessage = newmessage;
bw.write(sendMessage + "\n");
bw.flush();
System.out.println("Message sent to client: "+sendMessage);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
}
}
}
}
主要功能文件代码:
import java.io.IOException;
public class DirectMessengerCombined
{
public static void main(String[] args) throws IOException
{
DirectMessengerClient Client1 = new DirectMessengerClient();
// Thread t1 = new Thread(Client1);
DirectMessengerServer Server1 = new DirectMessengerServer(args);
//DirectMessengerServer Server1 = new DirectMessengerServer(args[1], null, 0);
for (int i = 0; i < args.length; i++)
{
if(!args[0].equals("-l"))
{
Client1.ClientRun(args);
}
switch (args[0].charAt(0))
{
case '-':
if(args[0].equals("-l"))
{
Server1.ServerRun(args);
}
}
i=args.length + 20;
}
}
}
我的问题是:在代码中接受连接的正确位置在哪里,这样两个运行方法都可以像两者都连接一样工作?
答案 0 :(得分:0)
通常你会把它放在自己的线程中的一个循环中,你将为每个接受的连接启动一个新的线程。