它是一个简单的客户端 - 服务器程序,客户端发送文本,服务器在控制台上显示它。这是我的代码:
MyServer的
SELECT *
FROM fixtures f
LEFT JOIN fixture_channel_join fcj ON f.fixtures_id = fcj.fixture_id
LEFT JOIN channel c ON fcj.channel_id = c.id
WHERE f.matchday = '1'
MyClient
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String args[]) throws IOException
{
ServerSocket socket=new ServerSocket(4444);
Socket s=socket.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String text;
while( (text=in.readLine())!=null)
{ System.out.println(text);
}
}
MyServer 类中的 readLine()语句中会出现异常。 这是我得到的例外:
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String args[]) throws IOException
{ Socket socket=new Socket("localhost",4444);
PrintWriter out=new PrintWriter(socket.getOutputStream());
out.println("c:/users/pinder/desktop/happy.txt");
}
}
答案 0 :(得分:2)
SocketException
是IOException
(位于throws
方法的main()
条款中。由于您没有正确关闭连接,因此读者投了这个错误,它是由启动器打印的。
如果您在另一端正在读取连接时close()
套接字,则您不会收到此错误。 e.g。
public class MyServer {
public static void main(String args[]) throws IOException {
try (ServerSocket socket = new ServerSocket(4444);
Socket s = socket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()))) {
String text;
while ((text = in.readLine()) != null) {
System.out.println(text);
}
}
}
}
public class MyClient {
public static void main(String[] args) throws IOException {
try (Socket socket = new Socket("localhost", 4444);
PrintWriter out = new PrintWriter(socket.getOutputStream())) {
out.println("c:/users/pinder/desktop/happy.txt");
}
}
}
如果我运行MyServer,然后运行MyClient,服务器将打印
c:/users/pinder/desktop/happy.txt