Socket協議網上介紹的有不少了,就不在多此一舉了,本文主要編寫一個小Demo,介紹下它具體實現java
一:Socket服務器端服務器
package com.founderit;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class HelloServer {
public static void main(String[] args) {
InputStream in = null;
OutputStream out = null;
try {
ServerSocket serverSocket= new ServerSocket(1234);
while (true){
Socket socket= serverSocket.accept();
in=socket.getInputStream();
InputStreamReader reader= new InputStreamReader(in);
StringBuilder builder=new StringBuilder();
for (int c = reader.read(); c != -1; c = reader.read()) {
builder.append((char)c);
}
System.out.println("收到的客戶端請求爲:-------"+builder.toString());
out=socket.getOutputStream();
out.write("請求已接收,over".getBytes());
out.flush();
socket.shutdownOutput();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
二:Socket客戶端
package com.founderit.controller;
import java.io.*;
import java.net.Socket;
public class SocketClient {
public static void main(String[] args) {
OutputStream out=null;
InputStream in=null;
Socket socket=null;
try {
socket=new Socket("localhost",1234);
out=socket.getOutputStream();
out.write("呼叫服務器,收到請回答".getBytes());
socket.shutdownOutput();
//獲取輸入流,並讀取服務器端的響應信息
in=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String info=null;
while ((info=br.readLine())!=null){
sb.append(info);
}
System.out.println("收到服務端回覆:"+sb.toString());
br.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.flush();
out.close();
in.close();
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
首先執行Socket服務端,它會一直運行等待接收請求,建立socketServer須要綁定端口,這個端口需須要與客戶端一致
而後執行Socket客戶端,它會發送Socket請求給服務端並接收服務器端反饋,運行效果以下
須要注意的是不管是客戶端與服務端,執行write()方法後最好執行socket.shutdownOutput()方法關閉輸出流,否則有概率寫入不成功(緣由未知)多線程
----------------------------------------------------------------------------------------------------------------------我是分割線---------------------------------------------------------------------------------------------------------------------------------------------上面的Demo只是個單機版,在實際工做中不會只有一個客戶端請求,下面咱們將改造Socket服務器端,使其支持多線程app
Socket服務器端socket
首先抽調具體實現,新建個具體實現類ide
package com.founderit;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;
public class HelloRunable implements Runnable{
Socket socket;
InputStream in = null;
OutputStream out = null;
public HelloRunable(Socket clientSocket){
this.socket=clientSocket;
}
@Override
public void run() {
try {
in=socket.getInputStream();
InputStreamReader reader= new InputStreamReader(in);
StringBuilder builder=new StringBuilder();
for (int c = reader.read(); c != -1; c = reader.read()) {
builder.append((char)c);
}
System.out.println("收到的客戶端請求爲:-------"+ new Date().toString()+ builder.toString());
out=socket.getOutputStream();
out.write("請求已接收,over".getBytes());
out.flush();
socket.shutdownOutput();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
in.close();
out.flush();
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
接收類
package com.founderit;import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class HelloServer { public static void main(String[] args) { try { ServerSocket serverSocket= new ServerSocket(1234); while (true){ Socket socket=serverSocket.accept(); new Thread(new HelloRunable(socket)).start(); } } catch (IOException e) { e.printStackTrace(); } }}改造後,Socket服務端接收到請求後,會直接開啓一個新線程執行任務