一.socket
这个类实例化需要两个参数
一个是IP地址,一个是int
型的端口号
ct = new Socket(IP, Port);
二.ServerSocket
这个是服务器端的套接字
这个类的实例化只要一个端口号参数即可
socketforclient = new ServerSocket(1999);
三.建立连接
建立通讯主要是两个部分
一个是服务器端,一个是客户端
在服务器端
我们要先使用一个ServerSocket
实例化一个对象
这个对象使用和客户端一样的端口号来作为参数进行实例化
socketforclient = new ServerSocket(1999); // 返回客户端是1999的套接字
之后使用accept()
函数建立连接,返回一个和这个端口号一样的Socket
对象
将这个对象赋值给服务器端的Socket
socketonserver = socketforclient.accept(); // 建立连接
这个过程相当于我们利用ServerSocket
作为海关,迎接了一个来自客户端且端口号一致的Socket
使者
然后这个使者赋值给自己的使者
之后靠这个使者来传输信息
// 接受发出数据流
in = new DataInputStream(socketonserver.getInputStream());
out = new DataOutputStream(socketonserver.getOutputStream());
(in 和 out 都是数据流)
在客户端
我们只要实例化一个Socket
类就行了
ct = new Socket(IP, Port);
in = new DataInputStream(ct.getInputStream());
out = new DataOutputStream(ct.getOutputStream());
四.完整代码
服务器端
package Platform;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
class ServerGUI extends JFrame
{
JTextArea showArea; //显示文本框
JTextField sendfield; //端口文本框
serthread st; // 线程
Startbtnpolice startbtnpolicebp;
Sentbtnpolice sentbtnpolice;
JButton sendbtn;
// 开始按钮监听器
private class Startbtnpolice implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
showArea.append("Server starting… "+"\n" );
}
}
// 发送按钮监听器
private class Sentbtnpolice implements ActionListener //
{
public void actionPerformed(ActionEvent e)
{
st.sentmssage(sendfield.getText());
showArea.append("Server: " + sendfield.getText() + "\n");
sendfield.setText("");
}
}
// 文本监听器
private class textpolice implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
st.sentmssage(sendfield.getText());
showArea.append("Server: " + sendfield.getText() + "\n");
sendfield.setText("");
}
}
public void setthread(serthread st)
{
this.st = st;
}
public ServerGUI()
{
super("服务器");
Container contain = getContentPane();
// 实例化监听器
startbtnpolicebp = new Startbtnpolice();
sentbtnpolice = new Sentbtnpolice();
// 显示文本框设置
showArea = new JTextArea(5,20);
JScrollPane js = new JScrollPane(showArea);
contain.add(js);
// 顶端布局设置
JPanel upPanel = new JPanel();
upPanel.setLayout(new FlowLayout());
JButton startbtn = new JButton("Start");
startbtn.addActionListener(startbtnpolicebp);
JLabel portlabel=new JLabel("Port:",SwingConstants.LEFT);
JTextField portfield=new JTextField(30);
portfield.setText("1999");
upPanel.add(portlabel);
upPanel.add(portfield);
upPanel.add(startbtn);
contain.add(upPanel, BorderLayout.NORTH);
// 底端布局设置
JPanel downpanel = new JPanel();
downpanel.setLayout(new FlowLayout());
sendfield = new JTextField(30);// 发送文本框
sendfield.addActionListener(new textpolice());
sendbtn = new JButton("Say");
sendbtn.addActionListener(sentbtnpolice);
downpanel.add(new JLabel("Say:"));
downpanel.add(sendfield);
downpanel.add(sendbtn);
contain.add(downpanel, BorderLayout.SOUTH);
setSize(500, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class serthread extends Thread
{
Socket socketonserver; // 自身的套接字
ServerSocket socketforclient; // 返回客户端的套接字
DataInputStream in; // 输入数据流
DataOutputStream out; // 输出数据流
ServerGUI gui; // 利用gui发送消息,使其显示在文本框中
public serthread(ServerGUI Gui)
{
try
{
gui = Gui;
gui.setthread(this); // 获得线程,最后利用线程在监听器中发送消息,其实在实例化主线程的st,进而完成监听器的操作
socketforclient = new ServerSocket(1999); // 返回客户端是1999的套接字
socketonserver = socketforclient.accept(); // 建立连接
// 接受发出数据流
in = new DataInputStream(socketonserver.getInputStream());
out = new DataOutputStream(socketonserver.getOutputStream());
}
catch(IOException e)
{
System.out.println(e);
}
}
public void run()
{
String massage;
// 不断获取信息
while(true)
{
try
{
massage = in.readUTF();
gui.showArea.append(massage + "\n");
}
catch(Exception e)
{
System.out.println(e);
break;
}
}
}
// 发送消息
public void sentmssage(String massage)
{
try
{
out.writeUTF("Server: " + massage + "\n");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public class Serve
{
public static void main(String args[])
{
ServerGUI sGUI = new ServerGUI();
serthread st = new serthread(sGUI);
st.start();
}
}
客户端
package Platform;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
class ClientGUI extends JFrame
{
JTextArea showArea; // 输出文本框
JTextField sendfield; // 发送框
clithread ct; // 自定义客户端线程
JTextField ipfield; // IP地址输入框
JTextField portfield; // Port发送框
JButton connectbtn; // 连接按钮
Connectbtnpolice connectbtnpolice; // 连接按钮监听器
Sendbtnpolice sendbtnpolice; // 发送按钮监听器
// 连接按钮监听器
private class Connectbtnpolice implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println(portfield.getText());
if(portfield.getText().equals( "1999"))
{
showArea.append("Connect to server…" + "\n");
// 连接成功以后开始创造线程
ct = new clithread(ipfield.getText(), 1999, ClientGUI.this);
ClientGUI.this.setthread(ct);
showArea.append("Client connected…" + "\n");
}
else
{
showArea.append("非法端口" + "\n");
}
}
}
// 发送按钮监听器
private class Sendbtnpolice implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ct.sentmessage(sendfield.getText());
showArea.append("Client: " + sendfield.getText() + "\n");
sendfield.setText("");
}
}
// 文本监听器
private class textpolice implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ct.sentmessage(sendfield.getText());
showArea.append("Client: " + sendfield.getText() + "\n");
sendfield.setText("");
}
}
//构造函数
public void setthread(clithread ct)
{
this.ct = ct;
}
public ClientGUI()
{
super("客户端");
// 布局
Container contain = getContentPane(); // 获得容器面板
contain.setLayout(new BorderLayout());
// 实例化监听器
connectbtnpolice = new Connectbtnpolice();
sendbtnpolice = new Sendbtnpolice();
// 对话文本框
showArea = new JTextArea(10,30);
contain.add(new JScrollPane(showArea), BorderLayout.CENTER);
// 顶端布局
JPanel upPanel = new JPanel();
upPanel.setLayout(new FlowLayout());
upPanel.add(new JLabel("Server IP:"));
ipfield = new JTextField(15);
ipfield.setText("127.0.0.1");
upPanel.add(ipfield);
upPanel.add(new JLabel("Serve Port:"));
portfield =new JTextField(8);
upPanel.add(portfield);
connectbtn = new JButton("Connect");
connectbtn.addActionListener(connectbtnpolice);
upPanel.add(connectbtn);
contain.add(upPanel, BorderLayout.NORTH);
// 底端布局
JPanel downpanel = new JPanel();
downpanel.setLayout(new FlowLayout()); // 面板
sendfield = new JTextField(30);//发送文本框
JButton sendBtn = new JButton("Say"); // 发送按钮
sendBtn.addActionListener(sendbtnpolice);
sendfield.addActionListener(new textpolice());
downpanel.add(new JLabel("Say:"));
downpanel.add(sendfield);
downpanel.add(sendBtn);
contain.add(downpanel, BorderLayout.SOUTH);
setSize(500, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class clithread extends Thread
{
Socket ct; // 自己的套接字
ClientGUI cGUI; // 调用窗口显示文本
DataInputStream in;
DataOutputStream out;
// 构造函数
public clithread(String IP, int Port, ClientGUI cgui)
{
try
{
cGUI = cgui;
ct = new Socket(IP, Port);
in = new DataInputStream(ct.getInputStream());
out = new DataOutputStream(ct.getOutputStream());
}
catch (Exception e)
{
System.out.println(e);
}
start();
}
public void run()
{
String message;
// 获取信息
while(true)
{
try
{
message = in.readUTF();
// System.out.println("接受到了");
cGUI.showArea.append(message);
}
catch(Exception e)
{
System.out.println(e);
break;
}
}
}
// 发送消息
public void sentmessage(String message)
{
try
{
out.writeUTF("Client: " + message);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
public class Client
{
public static void main(String[] args)
{
ClientGUI cGUI = new ClientGUI();
}
}