我一直被这个问题困扰,不知道如何解决。
我尝试使用 java 套接字编程在服务器和客户端之间创建通信。我使用此链接中的源代码用于服务器和此链接用于客户端来创建我的应用程序并将其分为 2 个项目包(客户端和服务器)。但是对于客户端,我将这个源代码修改为几个类,因为我想在这一端添加一个库。
当我尝试使用此修改后的源代码将数据从客户端发送到服务器时,就会出现问题。来自服务器的套接字总是发送一条消息“读取失败”,但是当我使用原始源代码时它可以正常工作(可以读取)。
这是服务器端的源代码(服务器包)
package server;
/**
*
* @author IRVAN AHADI
*/
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
class ClientWorker implements Runnable {
private Socket client;
private JTextArea textArea;
ClientWorker(Socket client, JTextArea textArea) {
this.client = client;
this.textArea = textArea;
}
public void run(){
String line;
BufferedReader in = null;
PrintWriter out = null;
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.out.println("in or out failed");
System.exit(-1);
}
while(true){
try{
line = in.readLine();
//Send data back to client
out.println(line);
textArea.append(line);
} catch (IOException e) {
System.out.println("Read failed");
System.exit(-1);
}
}
}
}
class SocketThrdServer extends JFrame{
JLabel label = new JLabel("Text received over socket:");
JPanel panel;
JTextArea textArea = new JTextArea();
ServerSocket server = null;
SocketThrdServer(){ //Begin Constructor
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
getContentPane().add(panel);
panel.add("North", label);
panel.add("Center", textArea);
} //End Constructor
public void listenSocket(){
try{
server = new ServerSocket(4444);
System.out.println("succes crate socket");
} catch (IOException e) {
System.out.println("Could not listen on port 4444");
System.exit(-1);
}
while(true){
ClientWorker w;
try{
w = new ClientWorker(server.accept(), textArea);
Thread t = new Thread(w);
t.start();
System.out.println("succes accept socket");
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}
}
}
protected void finalize(){
//Objects created in run method are finalized when
//program terminates and thread exits
try{
server.close();
} catch (IOException e) {
System.out.println("Could not close socket");
System.exit(-1);
}
}
public static void main(String[] args){
SocketThrdServer frame = new SocketThrdServer();
frame.setTitle("Server Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}
}
这是客户端的源代码(客户端包)
- 客户端.java
包客户端;
import java.io.*;
import java.net.*;
public class Client{
Socket socket;
PrintWriter out = null;
BufferedReader in = null;
String message = null;
Client(){
try{
socket = new Socket("IRVANA-PC",4444);
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Socket created and now connected to "+socket.getPort());
}catch(UnknownHostException e){
System.out.println("Unknown host!");
System.exit(1);
}catch(IOException e){
System.out.println("No I/O");
System.exit(1);
}
}
Client(String host, int port){
try{
socket = new Socket(host, port);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Socket created and now connected to "+socket.getPort());
}catch(UnknownHostException e){
System.out.println("Unknown host!");
System.exit(1);
}catch(IOException e){
System.out.println("No I/O");
System.exit(1);
}
}
public void sendData(String a){
out.println(a);
}
public void getData(){
try{
String line = in.readLine();
System.out.println("Text received :" + line);
} catch (IOException e){
System.out.println("Read failed");
System.exit(1);
}
}
public void disconnect(){
try{
this.socket.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
}
2.Clientside.java(客户端包的主类)
package client;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.cmd.SigarCommandBase;
import org.hyperic.sigar.Sigar;
public class ClientSide {
public ClientSide(){
}
public static void main(String[] args) throws Exception{
ClientSide client = new ClientSide();
if ((args.length > 0) &&(args.length == 2))
{
int port = Integer.parseInt(args[1]);
Client clientNetwork = new Client(args[0],port);
System.out.println(clientNetwork.socket.getPort());
String data = client.persentaseCPU();
System.out.println(data);
clientNetwork.sendData(data);
}
else{
Client clientNetwork = new Client();
String data = client.persentaseCPU();
clientNetwork.sendData(data);
}
}
public String persentaseCPU() throws Exception{
CpuNfo cpunfo = new CpuNfo();
Sigar a = new Sigar();
CpuPerc cpus = a.getCpuPerc();
String percentage="idle "+CpuPerc.format(cpus.getIdle());
return percentage;
}
}
我只是想知道为什么当我clientNetwork.sendData(data);
从 Clientside.java 调用它不起作用时,我错过了什么吗?我是否sendData(String a)
在 Client.java 中正确创建了通过客户端套接字发送数据的方法?
非常感谢!