Java套接字。服务器 - 客户端通信 [英] Java Sockets. Server-Client communication
问题描述
我正在尝试将客户端与gui连接到一个没有gui的服务器。正在进行连接,但我无法看到这两个应用之间的任何消息。
(我应该在客户端获取SERVER,在服务器中使用CLIENT)
I am trying to connect a client with gui with a server withoout gui. Connection is being done, but i cant see any messages between these two apps. (i should get SERVER HERE in client, and CLIENT HERE in server)
客户端连接代码:
@Override
public void ClientRunning(){
try {
connectToServer();
setStreams();
ClientRun();
}catch(EOFException oefException){
showMessage("\n Client terminated the connection\n");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
close();
}
}
public void connectToServer() throws IOException{
showMessage("Attempting Connection... \n");
connection = new Socket(InetAddress.getByName(serverIP),6789);
showMessage("Connected to: "+ connection.getInetAddress().getHostName());
}
public void setStreams() throws IOException{
output = new PrintWriter(connection.getOutputStream(),true);
output.flush();
input= new BufferedReader(new InputStreamReader(connection.getInputStream()));
showMessage("\n Streams are now set. \n");
}
public void close(){
showMessage("\n closing...");
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
public void showMessage(final String text){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
cwindow.append(text);
}
});
}
public void sendMessage(String message){
output.write("CLIENT - "+message);
output.flush();
showMessage("\nCLIENT - "+message);
}
private void ClientRun() throws IOException{
String message="CLIENT HERE!";
sendMessage(message);
do{
try{
message=input.readLine();
showMessage("\n"+message);
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}
}while(message!="EXIT");
}
(输入和输出在GUI类中定义,其中此Client类被扩展to。定义为受保护的BufferedReader输入;
受保护的PrintWriter输出;)
(input and output are defined in GUI class in which this Client class is extended to. Defined as "protected BufferedReader input; protected PrintWriter output;")
此外,服务器代码:
public class ServerClass {
private ServerSocket server;
private Socket connection;
private BufferedReader input;
private BufferedWriter output;
public void startServer(){
try{
server=new ServerSocket(6789,100);
while(true){
try{
waitForConnection();
setStreams();
ServerRunning();
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}finally{
close();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}
private void waitForConnection() throws IOException{
showMessage("Waiting for someone to connect... \n");
connection=server.accept();
showMessage("Now connected to "+ connection.getInetAddress().getHostName());
}
private void setStreams() throws IOException{
output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
output.flush();
input= new BufferedReader(new InputStreamReader(connection.getInputStream()));
showMessage("\n Streams are now set. \n");
}
public void ServerRunning() throws IOException{
String message="SERVER HERE!";
sendMessage(message);
do{
try{
message=input.readLine();
showMessage("\n"+message);
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}
}while(message!="EXIT");
}
private void close(){
showMessage("\n Closing connections... \n");
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
private void showMessage(String text){
System.out.println(text);
}
private void sendMessage(String message){
try{
output.write("SERVER - "+message);
output.flush();
showMessage(message);
}catch(IOException ioException){
System.out.println("\n ERROR!");
}
}
连接似乎没问题,所以我不知道搞错了。任何帮助将不胜感激。
Connection seems to be ok, so i don't get whats wrong. Any help would be appreciated.
PS:我也在服务器中尝试过PrintWriter,并尝试在流语句中尝试catch,问题仍然存在。
PS: i also tried PrintWriter in server as well and also tried a try catch in stream statement, the problem remains.
推荐答案
您正在使用 readLine()
,它需要换行符。将 \ n
添加到您要发送的邮件中,或者不使用 readLine()
。
You're using readLine()
which expects a newline character. either add a \n
to the messages you are sending or don't use readLine()
.
这篇关于Java套接字。服务器 - 客户端通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!