我有一个非常奇怪的问题,如果我从服务器/客户端向服务器/客户端发送消息,他们将不会在控制台中发送。 我已经尝试了整整一天来修复它并在之前用谷歌搜索它,但我无法找到问题的答案,这就是我在这里问的原因。
My Code:
Serversocket:
package me.jackboyplay.sockets_server;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author JackboyPlay
*/
public class Server {
public static void main(String[] args) {
ServerSocket socket = null;
try {
socket = new ServerSocket(1243);
System.out.println("Gestartet...");
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
if(socket == null){
System.out.println("NULL -> Aus");
return;
}
while(true){
try {
new Clients(socket.accept()).start();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
package me.jackboyplay.sockets_server;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author JackboyPlay
*/
public class Clients extends Thread{
Socket socket;
DataInputStream dis;
PrintStream dos;
public Clients(Socket socket){
this.socket = socket;
try {
dis = new DataInputStream(socket.getInputStream());
dos = new PrintStream(socket.getOutputStream());
} catch (IOException ex) {
Logger.getLogger(Clients.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void run(){
System.out.println("Thread started...");
String receive = null;
while(true){
try {
receive = dis.readLine();
} catch (IOException ex) {
Logger.getLogger(Clients.class.getName()).log(Level.SEVERE, null, ex);
}
if(receive != null){
System.out.println(receive);
dos.println("Out");
dos.flush();
}
}
}
}
Client:
package me.jackboyplay.sockets_client;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author JackboyPlay
*/
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = null;
try {
socket = new Socket("localhost", 1243);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
if(socket == null){
return;
}
DataInputStream dis = null;
PrintWriter dos = null;
try {
dis = new DataInputStream(socket.getInputStream());
dos = new PrintWriter(socket.getOutputStream());
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
String receive = null;
while(true){
Scanner s = new Scanner(System.in);
try {
receive = dis.readLine();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
if(receive != null){
dos.println("Hallo");
dos.flush();
try {
receive = dis.readLine();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(receive);
}
}
}
}
答案 0 :(得分:0)
两端从套接字接收开始。你陷入僵局。重新考虑您的申请协议