對(duì)于Java Socket編程而言涂炎,有兩個(gè)概念秃流,一個(gè)是ServerSocket赂蕴,一個(gè)是Socket。服務(wù)端和客戶端之間通過Socket建立連接舶胀,之后它們就可以進(jìn)行通信了概说。首先ServerSocket將在服務(wù)端監(jiān)聽某個(gè)端口,當(dāng)發(fā)現(xiàn)客戶端有Socket來試圖連接它時(shí)嚣伐,它會(huì)accept該Socket的連接請(qǐng)求糖赔,同時(shí)在服務(wù)端建立一個(gè)對(duì)應(yīng)的Socket與之進(jìn)行通信。這樣就有兩個(gè)Socket了轩端,客戶端和服務(wù)端各一個(gè)放典。
Socket編程
Server端五步
1.啟動(dòng)Server ServerSocket serverSocket = new ServerSocket(port)
2.偵聽連接 Socket socket = serverSocket.accept();
3.讀取數(shù)據(jù) BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
input.readLine();
4.寫數(shù)據(jù) BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
output.write();
5.關(guān)閉連接 socket.close();
serverSocket.close();
Client端四步
1.啟動(dòng)Server ServerSocket serverSocket = new ServerSocket(port)
2.偵聽連接 Socket socket = serverSocket.accept();
3.讀取數(shù)據(jù) BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
input.readLine();
4.寫數(shù)據(jù) BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
output.write();
5.關(guān)閉連接 socket.close();
serverSocket.close();
下面實(shí)現(xiàn)一個(gè)簡(jiǎn)單的點(diǎn)對(duì)點(diǎn)的聊天,輸入“bye”時(shí)基茵,聊天結(jié)束
Sever.java
package com.day1006.oo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String[] args) {
try {
System.out.println("正在啟動(dòng)服務(wù)器...");
ServerSocket serverSocket = new ServerSocket(1000);
System.out.println("服務(wù)器啟動(dòng)成功...");
System.out.println("等待連接...");
Socket socket = serverSocket.accept();
System.out.println("連接成功...");
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
Scanner sc = new Scanner(System.in);
boolean shouldContinue =true;
String clientMsg;
String serverMsg = null;
while(shouldContinue){
clientMsg = input.readLine();
System.out.println("client:"+clientMsg);
if(!"bye".equals(clientMsg)){
System.out.print("請(qǐng)輸入回復(fù):");
serverMsg = sc.nextLine();
System.out.println("server:"+serverMsg);
output.write(serverMsg+"\r");
output.flush();
}
if("bye".equals(serverMsg)|| "bye".equals(clientMsg)){
shouldContinue = false;
}
}
serverSocket.close();
sc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Client.java
package com.day1006.oo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
System.out.println("等待連接...");
Socket socket = new Socket("localhost", 1000);
System.out.println("連接成功...");
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
Scanner sc = new Scanner(System.in);
boolean shouldContinue = true;
String clientMsg;
String serverMsg = null;
while (shouldContinue) {
clientMsg = sc.nextLine();
System.out.println("client:" + clientMsg);
output.write(clientMsg + "\r");
output.flush();
if (!"bye".equals(clientMsg)) {
serverMsg = input.readLine();
System.out.println("server:" + serverMsg);
}
if ("bye".equals(serverMsg) || "bye".equals(clientMsg)) {
shouldContinue = false;
}
}
sc.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}