Socket 通信
1-Socket:socket的通信建立,由两部分来实现,分别是:ServerSocket 和 Socket;ServerSocket负责监听 (accept)Socket端的连接请求,并接收数据流(根据指定 port 和 host);在ServerSocket端也建立一个Socket来和Socket端进行通信;
服务端实现-
package com.bdc;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class SocketChatServers {
private int port;//设置 连接 端口
public SocketChatServers(int port){
this.port = port;
}
//业务处理
public void service() throws IOException{
int i = 0;
ServerSocket serverSocket = new ServerSocket(port,3);//建立 Socket 服务
while(true){
Socket socket = serverSocket.accept();//等待 Socket 连接
System.out.println("第"+(++i)+"个客户已连接...");
new ServerSocketThread(socket).start();
}
}
public static void main(String[] args) throws IOException {
new SocketChatServers(9001).service();
}
}
class ServerSocketThread extends Thread{
private Socket socket ;
public ServerSocketThread(Socket socket){
this.socket = socket;
}
@Override
public void run(){
DataInputStream dis = null;
DataOutputStream dos = null;
try {
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
Scanner scan = new Scanner(System.in);
while(true){
String sendToClient = scan.nextLine();
dos.writeUTF(sendToClient);
System.out.println("服务器 说:"+sendToClient);
String accept = dis.readUTF();
System.out.println("客户端 说"+accept);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端实现-
</pre></p><p align="left"><span style="font-size:18px;"><span style="font-size:24px;"><span style="font-size:18px;"></span></span></span><pre name="code" class="java">package com.bdc;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class SocketChatClient {
private String host;//主机 ip
private int port;//连接 端口
public SocketChatClient(String host,int port){
this.host = host;
this.port = port;
}
public void client() throws ConnectException{
Socket socket = null ;//建立 Socket 连接
DataInputStream dis = null;
DataOutputStream dos = null;
try {
socket = new Socket(host,port);//连接 Socket服务器
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
Scanner scan = new Scanner(System.in);
while(true){
String sendToServer = scan.nextLine();
dos.writeUTF(sendToServer);
System.out.println("客户端 说: "+sendToServer);
String accept = dis.readUTF();
System.out.println("服务器 说: "+accept);
}
}catch (ConnectException e) {
System.err.println("**** 连接 异常 ****");
}catch(IOException e){
System.err.println("**** IO 异常 ****");
}finally{
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.err.println("请输入要登录的host :");
String host = scan.nextLine();
System.err.println("请输入连接 Port :");
int port = scan.nextInt();
try {
new SocketChatClient(host, port).client();
} catch (ConnectException e) {
System.err.println("**** 连接 异常 ****");
}
}
}
请输入要登录的host :
localhost
请输入连接 Port :
9001
hello
客户端 说: hello