Java套接字。服务器-客户端通信
我试图用没有gui的服务器连接带有gui的客户端。连接已完成,但我看不到这两个应用程序之间的任何消息。(我应该在客户端中找到SERVER
HERE,在服务器中找到CLIENT HERE)
客户端连接代码:
@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类中定义。定义为“受保护的BufferedReader输入;受保护的PrintWriter输出;”)
另外,服务器代码:
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!");
}
}
连接似乎还可以,所以我不会弄错。任何帮助,将不胜感激。
PS:我也尝试在服务器中使用PrintWriter,并且还尝试在流语句中尝试try catch,问题仍然存在。
-
使用套接字的客户端-服务器-客户端通信
2021-01-30 关注 0 浏览120 1答案
-
套接字Java客户端-Python服务器
2021-01-29 关注 0 浏览92 1答案
-
java.net.SocketException:套接字已关闭TCP客户端服务器通信
2021-01-30 关注 0 浏览100 1答案
-
Java套接字,客户端和服务器
2021-02-01 关注 0 浏览103 1答案
-
带套接字的Java客户端/服务器应用程序?
2021-01-30 关注 0 浏览92 1答案
-
Java多线程客户端/服务器-java.net.SocketException:套接字已关闭
2021-01-29 关注 0 浏览164 1答案
-
是否可以在客户端和服务器端都关闭Java套接字?
2021-01-30 关注 0 浏览119 1答案
-
Java套接字:检查客户端是否能够从服务器接收消息
2021-01-29 关注 0 浏览108 1答案
-
Java客户端和C ++服务器通过TCP套接字发送和接收
2021-01-30 关注 0 浏览79 1答案
-
如何从服务器获取客户端套接字的确认?
2021-01-30 关注 0 浏览89 1答案