您好,登錄后才能下訂單哦!
通過ServerSocket與Socket實現的TCP的通信,這個例子配合了swing使用,其實作者之前發過一篇ServerSocker與Socket的通信,在這里,這邊文章就是在這個例子上修改而成的.
其實原理很簡單的,就是一個服務端一個客戶端,服務端這邊創建好ServerSocket后,使用accept()堵塞一直等待客戶端發送socket.
好吧,不廢話了.進正題.
swing的部分就不說了,說一下主要操作.
ServerSocket serverSocket = null;
serverSocket = new ServerSocket(12345);//一個不被占用的端口即可
構造方法的參數是端口,默認使用本地ip.
ServerSocket有三個構造方法.
ServerSocket(int port);
ServerSocket(int port,int backlog);
ServerSocker(int poer,int backlog,InetAddress ip);
詳情可以看這里
Socket socket = serverSocket.accept();
返回一個Socket(),會一直等待,處于阻塞狀態.
一般設置一個while(true)循環接收.
使用DataInputStream與DataOutputStream:
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
DataOutputStram outputStream = new DataOutputStream(socket.getOutputStream());
然后使用read與write方法即可.
注意關閉的時候要等輸入輸出完成后再一起關閉,就是說,不能這樣:
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String data = inputStream.readUTF();
inputStream.close();
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF("ok");
outputStream.close();
這樣的話getInputStream()沒有問題,但是到了getOutputStream()的時候就有問題了,會提示異常:
Socket is closed.
所以解決辦法就是等輸出完之后再一起關閉:
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String data = inputStream.readUTF();
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF("ok");
inputStream.close();//等輸出完成后再關閉
outputStream.close();
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;
public class Server {
public static void main(String[] args)
{
JFrame frame = new JFrame("Server");
JPanel panel = new JPanel();
JTextArea text = new JTextArea();
panel.add(text);
frame.setContentPane(panel);
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(12345);
while (true)
{
Socket socket = serverSocket.accept();
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
text.setText(inputStream.readUTF());
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF("ok");
inputStream.close();
outputStream.close();
socket.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
serverSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
客戶端的操作就是開啟一個Socket,再通過這個socket獲取對應的輸入輸出流就可以了.
Socket socket = new Socket("127.0.0.1",12345);//這個端口與服務端的端口對應
Socket有三個構造方法:
Socket(InetAddress ip,int port);
Socket(String ip,int port);
Socket(InetAddress ip,int port,InetAddress localIp,int localPort);
Socket(String ip,int port,InetAddress localIp,int localPort);
詳情可以看這里
同樣使用DataInputStream與DataOutputStream:
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.*;
import java.awt.GridLayout;
public class Client
{
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextArea text = new JTextArea();
JButton send = new JButton();
GridLayout layout = new GridLayout(2, 1, 0, 5);
frame.setTitle("Client");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(600, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(layout);
frame.add(text);
frame.add(send);
send.setText("send");
send.addActionListener(v ->
{
try
{
String host = "127.0.0.1";
int port = 12345;
Socket socket = new Socket(host, port);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF(text.getText());
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
text.setText(inputStream.readUTF());
outputStream.close();
inputStream.close();
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
});
}
}
先開啟服務端,再開啟客戶端,這里設置了服務端返回給客戶端的信息為"ok".
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。