Java Socket Program

server
client
Source code:
Client
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client1 extends JFrame
{
private JTextField enter;
private JTextArea display;
PrintStream output;
DataInputStream input;
String message = "";
public Client1()
{
  super("Client");
  Container c = getContentPane();
  enter = new JTextField();
  enter.setEnabled(false);
  enter.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    sendData(enter.getText());
   }
  });
  c.add(enter,BorderLayout.NORTH);
  display = new JTextArea();
  c.add(new JScrollPane(display),BorderLayout.CENTER);
  setSize(300,150);
  show();
}
public void connect()
{
  Socket socket;
  try
  {
   display.setText("准备连接...\n");
   socket = new Socket(InetAddress.getByName(""),4321);
   display.append("连接到:"+socket.getInetAddress().getHostName());
   display.append("\n主机IP为:"+socket.getInetAddress().toString());
   output = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
   output.flush();
   input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
   enter.setEnabled(true);
   do
   {
    try
    {
     message = (String)input.readLine();
     display.append("\n"+message);
    }
    catch(IOException e)
    {
    display.append("\n无法获得信息");
    }
   }
   while(!message.equals("Server: end"));
   display.append("\n关闭连接");
   output.close();
   input.close();
   socket.close();
  }
  catch(EOFException eof)
  {
   System.out.println("服务器中断连接");
  }
  catch(IOException e)
  {
   e.printStackTrace();
  }
}
private void sendData(String s)
{
  try
  {
   message = s;
   output.println("Client: "+s);
   output.flush();
   enter.setText("");
  }
  catch(Exception e)
  {
   display.append("\n数据传输错误!");
  }
}
public static void main(String args[])
{
  Client1 app = new Client1();
  app.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
   }
  });
  app.connect();
}
Server
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server1 extends JFrame
{
private JTextField enter;
private JTextArea display;
PrintStream output;
DataInputStream input;
public Server1()
{
  super("Server");
  Container c = getContentPane();
  enter = new JTextField();
  enter.setEnabled(false);
  enter.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    sendData(enter.getText());
    }
   });
    c.add(enter,BorderLayout.NORTH);
    display=new JTextArea();
    c.add(new JScrollPane(display),BorderLayout.CENTER);
    setSize(300,150);
    show();
}
private void sendData(String s)
{
  try
  {
   output.println("Server: "+s);
   output.flush();
   enter.setText("");
  }
  catch (Exception e)
  {
   display.append("\nError writing object");
  }
}
public void connect()
{
  ServerSocket server;
  Socket socket;
  int counter=1;
  try
  {
   server = new ServerSocket(4321,10);
   while(true)
   {
    display.setText("等待客户端的请求\n");
    socket = server.accept();
    display.append("连接"+counter+"来自:"+socket.getInetAddress().getHostName());
    output = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
    output.flush();
    input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    String message = "Server Connection successful!";
    output.println(message);
    output.flush();
    enter.setEnabled(true);
    do
    {
     try
     {
      message = (String)input.readLine();
      display.append("\n"+message);
     }
     catch(IOException e)
     {
      display.append("Data Error");
     }
    }
    while(!message.equals("Client: end"));
    display.append("\n关闭连接");
    enter.setEnabled(false);
    output.close();
    input.close();
    socket.close();
    ++counter;
   }
  }
  catch(EOFException eof)
  {
   System.out.println("Client terminated connection");
  }
  catch(IOException e)
  {
   e.printStackTrace();
  }
}
public static void main(String args[])
{
  Server1 app = new Server1();
  app.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    System.exit(0);
    }
   });
  app.connect();
}
}

你可能感兴趣的:(java,职场,休闲)