我有一个客户端服务器程序,其中servercoket将接受来自客户端的连接,并且客户端可以从服务器接收消息,反之亦然。 客户:
public class ChatClient {
private final String serverName;
private final int serverPort;
private Socket socket;
private OutputStream serverOut;
private InputStream serverIn;
private BufferedReader bufferedInputStream;
//private ArrayList<UserStatusListener> userListners = new ArrayList<UserStatusListener>();
private ChatClient(String serverName, int serverPort) {
super();
this.serverName = serverName;
this.serverPort = serverPort;
}
public static void main(String[] args) throws IOException {
ChatClient client = new ChatClient("localhost", 8818);
// Make sure serverboot is running and listenig first
if (! client.connect()) {
System.out.println("Connection Failed. Is ServerBoot running/listening?");
}else {
System.out.println("Connection succesful");
client.login("guest");
}
}
private boolean connect() {
// TODO Auto-generated method stub
try {
this.socket = new Socket(serverName, serverPort);
// get streams
this.serverOut = socket.getOutputStream();
this.serverIn = socket.getInputStream();
this.bufferedInputStream = new BufferedReader(new InputStreamReader(serverIn));
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
private void login (String login) throws IOException {
// send login to server
try {
String serverResponse = bufferedInputStream.readLine();
System.out.println("Server response: " + serverResponse);
while (bufferedInputStream.readLine() != null) {
System.out.println(bufferedInputStream.readLine());
}
String send = "Login : " + login;
serverOut.write(send.getBytes());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
代码段构成服务器:
try {
ServerSocket serverSocket = new ServerSocket(serverPortNumber);
while (true) {
// Listen for incoming connections and craete connection with accept
System.out.println("Waiting for client connection.... on localhost port " + serverPortNumber
+ ". \n Client connect via netcat localhost 8818");
Socket clientSocket = serverSocket.accept();// returns a client Socket object
System.out.println("Conenction established with " + clientSocket);
// Each new connection will be handled on a new thread and can run concurrenlty
ManageMultipleConnections multipleConnections = new ManageMultipleConnections(this, clientSocket);
clientList.add(multipleConnections);
multipleConnections.start();
}
//获取客户端套接字输入流
clientInput = clientSocket.getInputStream();
clientoutput = clientSocket.getOutputStream();
//写入客户端套接字
clientoutput.write((message).getBytes());
//尝试从客户端读取信息,但我们从未收到任何消息
BufferedReader br = new BufferedReader(new InputStreamReader(clientInput, ENCODING));
String inputLine;
String returnMessage;
String msg = br.readLine();
System.out.println(br.readLine());
while ((inputLine = br.readLine()) != null && inputLine != "") {....do stuff
任何输入表示赞赏。
答案 0 :(得分:0)
在尝试了几乎所有可能的解决方案一周之后,我发现在客户端通过outputtsteam发送到服务器的每条消息后添加回车和换行符都可以解决问题。
133222, Bears, Bears Stadium, 444333, 23, 35.3, 52
666222, Eagles, Eagles Stadium, 322222, 13, 56.3, 54
我不确定为什么这样做有效,因此任何可以阐明这一点的人都很棒。我在Windows 10操作系统上的Eclipse中使用java swing。