我对客户端-服务器编程比较陌生,我试图在 applet gui 上实现客户端和服务器之间的简单通信,它现在可以工作,但是当我运行客户端时出现异常
修改后的代码:
客户:
import java.awt.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Client extends JApplet{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea displayArea;
private Socket socket = null;
private DataInputStream input;
private DataOutputStream output;
public void init()
{
Container container = getContentPane();
// set up JTextArea to display
displayArea = new JTextArea();
displayArea.setEditable(false);
container.add(new JScrollPane(displayArea), BorderLayout.SOUTH );
try
{
// make connection
socket = new Socket(getCodeBase().getHost(), 5423);
// get streams
input = new DataInputStream( socket.getInputStream() );
output = new DataOutputStream( socket.getOutputStream() );
while(true)
{
output.writeUTF("hello from client"); //send message to server
output.flush();
processMessage(input.readUTF()); //process data from server
}
}
// catch problems setting up connection and streams
catch ( IOException ioException ) {
ioException.printStackTrace();
}
try
{
input.close();
output.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// process message received by client
private void processMessage(String message)
{
if (message.equals("hello") )
{
displayMessage("Connected to server");
}
}
private void displayMessage(final String messageToDisplay)
{
SwingUtilities.invokeLater(
new Runnable() {
public void run()
{
displayArea.append(messageToDisplay);
displayArea.setCaretPosition(displayArea.getText().length());
}
}
);
}
}
服务器:
import java.awt.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Server extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private ServerSocket serverSocket;
private JTextArea outputArea;
private Socket socket = null;
private DataInputStream input;
private DataOutputStream output;
public Server()
{
super("Server");
try
{
serverSocket = new ServerSocket(5423, 1);
}
catch(IOException ioException)
{
ioException.printStackTrace();
System.exit(1);
}
outputArea = new JTextArea();
getContentPane().add(outputArea, BorderLayout.CENTER);
outputArea.setText("Server awaiting connections\n");
setSize(300, 300);
setVisible(true);
}
private void doThis() throws IOException
{
try
{
while(true)
{
socket = serverSocket.accept();
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
output.writeUTF("hello");
outputArea.append(input.readUTF()); //receive data from client
output.flush();
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
output.close();
input.close();
socket.close(); //close connection to client
}
}
public static void main(String[] args) throws IOException
{
Server server = new Server();
server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
server.doThis();
}
}
异常内容如下:
java.net.SocketException:连接重置在 java.net.SocketInputStream.read(Unknown Source) 在 java.net.SocketInputStream.read(Unknown Source) 在 java.io.DataInputStream 的 java.net.SocketInputStream.read(Unknown Source) .readUnsignedShort(Unknown Source) at java.io.DataInputStream.readUTF(Unknown Source) at java.io.DataInputStream.readUTF(Unknown Source) at Client.init(Client.java:45) at sun.applet.AppletPanel.run(未知来源)在 java.lang.Thread.run(未知来源)