我试图创建一个来回发送消息的程序。我似乎已经这样做了;但是,为了使消息来回发送,客户端必须首先发送消息以使对话能够快速启动。
我想知道如何根据想要先发送邮件的人发送邮件。此外,他们似乎处于一种交替的对话方式,如果服务器或客户端发送两条直接消息,则会出现错误...我真的希望这一点有意义。
服务器
import java.io.*;
import java.net.*;
class Server
public static void main(String arg[]) throws IOException {
ServerSocket ss = null;
try{
ss = new ServerSocket(1111);
}catch(IOException e){
System.out.println("Failed");}
Socket sock = null;
try{
sock = ss.accept();
}catch(IOException io){
System.out.println("Socket Failed");}
System.out.println("Connection Successful..");
BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintStream output = new PrintStream(sock.getOutputStream());
BufferedReader ServerMessage = new BufferedReader(new InputStreamReader(System.in));
String Message;
while(true){
message= input.readLine();
System.out.println("Clien: " + message);
System.out.println("Server: ");
message = ServerMessage.readLine();
output.println(message);
System.out.println("Server: " + message);
}
}
}
客户:
import java.io.*;
import java.net.*;
class Server
public static void main(String arg[]) throws IOException {
Socket sock = null;
try{
sock = new Socket("127.0.0.1", 1111);
}catch(IOException io){
System.out.println("Socket Failed");}
System.out.println("Connection Successful..");
BufferedReader in = new BufferedReader(new `InputStreamReader(sock.getInputStream()));`
PrintStream out = new PrintStream(sock.getOutputStream());
BufferedReader ClientMessage = new BufferedReader(new InputStreamReader(System.in));
String Message;
while(true){
System.out.print("Client: ");
Message = ClientMessage.readLine();
output.println(Message);
System.out.println("Client: " + Message);
Message = in.readLine();
System.out.println("Server: " + Message);
}
}
}
答案 0 :(得分:1)
您实际需要的解决方案是"基于Peer的",并且无法在一个带有(普通)Java套接字的端口上运行。由于ServerSocket
"主要"方法是accept()
,而(客户端)Socket
的方法是connect()
:您必须启动两个程序(封装每个一个 Socket和一个 ServerSocket),accept
和connect
相互(在不同的端口上)。
(java.net。*)MultiCastSocket或DatagrammSocket中的一个也可能符合您的需求。
查看我对简单聊天客户端的想法":
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author xerx593
*/
public class SocketPeer {
public static void main(String args[]) throws IOException {
//args: <nickName> <server port> <client port>
startServer(Short.valueOf(args[1]));
startClient(args[0], Short.valueOf(args[2]));
}
private static void startServer(short port) throws IOException {
final Thread st = new Thread(() -> {
try {
//start the server...
final ServerSocket ss = new ServerSocket(port);
System.out.println("Listening for connections..");
//and (blockingly) listen for connections
final Socket sock = ss.accept();
//got one!
final BufferedReader input = new BufferedReader(new InputStreamReader(
sock.getInputStream()//remote input!
));
while (true) {// as long as connected, bad exception otherwise
String message1 = input.readLine();// read remote message
System.out.println(message1);// print to (local) console
}
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
ioe.printStackTrace(System.err);
}
});
st.start();
}
private static void startClient(String id, short port) {
final Thread ct = new Thread(() -> {
Socket sock = null;
while (sock == null) {
try {//..to connect with a server
sock = new Socket("127.0.0.1", port);
} catch (IOException io) {
System.out.println("Socket Failed.. retry in 10 sec");
try {//..retry in 10 secs
Thread.sleep(10000);
} catch (InterruptedException ex) {
ex.printStackTrace(System.err);
}
}
}
//sock != null -> connection established
try {
System.out.println("Connection Successful..");
final BufferedReader in = new BufferedReader(new InputStreamReader(
System.in //(local!) "keyboard"
));
//remote output
final PrintStream out = new PrintStream(sock.getOutputStream());
while (true) {
System.out.print(id + ">");
final String message1 = in.readLine();
out.println(id + ">" + message1);
}
} catch (IOException io) {
System.out.println("Socket Failed");
}
});
ct.start();
}
}
它在&#34; 127.0.0.1&#34;上进行了硬编码,但它可能适用于任何(可配置的)地址。您可以通过例如:
启动程序的两个实例$>java SocketPeer mickey 8081 8082
和
$>java SocketPeer minnie 8082 8081
这仍然基于你的代码,当然没有太多的错误处理或有用的帮助信息,但是一个很好的工作程序。好好享受! : - )
TODO&#39; S: