当前位置: 首页 > >

java socket client-server

发布时间:

一个客户端,一个服务端,客户端可以发消息到服务端


?


客户端代码client:


package com.lw.socket.client;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Client {

public static final String IP = "localhost";// 服务器地址
public static final int PORT = 8000;// 服务器端口号

public static void main(String[] args) {
Client client = new Client();
client.handler();
}

private void handler() {
try {
// 实例化一个Socket,并指定服务器地址和端口
Socket client = new Socket(IP, PORT);
// 开启两个线程,一个负责读,一个负责写
new Thread(new ClientReadThread(client)).start();
new Thread(new ClientWriteThread(client)).start();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 处理读操作的线程
*/
class ClientReadThread implements Runnable {
private Socket client;

public ClientReadThread(Socket client) {
this.client = client;
}

@Override
public void run() {
DataInputStream dis = null;
try {
while (true) {
// 读取客户端数据
dis = new DataInputStream(client.getInputStream());
String reciver = dis.readUTF();
System.out.println("服务器端返回过来的是:" + reciver);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
if (client != null) {
client = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* 处理写操作的线程
*/
class ClientWriteThread implements Runnable {
private Socket client;

public ClientWriteThread(Socket client) {
this.client = client;
}

@Override
public void run() {
DataOutputStream dos = null;
BufferedReader br = null;
try {
while (true) {
// 向客户端回复信息
dos = new DataOutputStream(client.getOutputStream());
System.out.print("客户端请输入: ");
// 键盘录入
br = new BufferedReader(new InputStreamReader(System.in));
String send = br.readLine();
// 发送数据
dos.writeUTF(send);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dos != null) {
dos.close();
}
if (br != null) {
br.close();
}
if (client != null) {
client = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

}

?


服务端代码server:


package com.lw.socket.server;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

public static final int PORT = 8000;// 监听的端口号

public static void main(String[] args) {
Server server = new Server();
server.init();
}

public void init() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(PORT);
System.out.println("启动服务器....");
int count = 0;
while (true) {
count++;
Socket client = serverSocket.accept(); // 等待客户端链接
System.out.println("客户端:"+InetAddress.getLocalHost()+"已连接到服务器" + ",连接数量:" + count);
// 一个客户端连接就开户两个线程处理读写
new Thread(new ServerReadThread(client)).start();
new Thread(new ServerWriteThread(client)).start();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 处理读操作的线程
*/
class ServerReadThread implements Runnable {
private Socket client;

public ServerReadThread(Socket client) {
this.client = client;
}

@Override
public void run() {
DataInputStream dis = null;
try {
while (true) {
// 读取客户端数据
dis = new DataInputStream(client.getInputStream());
String reciver = dis.readUTF();
System.out.println("客户端发过来的内容:" + reciver);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
if (client != null) {
client = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* 处理写操作的线程
*/
class ServerWriteThread implements Runnable {
private Socket client;

public ServerWriteThread(Socket client) {
this.client = client;
}

@Override
public void run() {
DataOutputStream dos = null;
BufferedReader br = null;
try {
while (true) {
dos = new DataOutputStream(client.getOutputStream()); // 向客户端回复信息
System.out.print("服务端请输入: ");

br = new BufferedReader(new InputStreamReader(System.in)); // 键盘录入
String send = br.readLine();

dos.writeUTF(send); // 发送数据
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dos != null) {
dos.close();
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}

?


?


?







相关资源:Socket编程的client



友情链接: