java网络编程____socket入门demo3
//服务端
package com.net.socket;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @info java_socket入门Demo3
* @author God 客户端与服务器端通信
* @see 参考资料 wind圣骑士blog
*/
public class TcpSocketServer {
public static void main(String[] args) {
try {
// 创建服务器端套接字
ServerSocket serverSocket = new ServerSocket(3000);
// 做一个循环一直监听
while (true) {
// 多客户端可做socket集合 noi的chanelSocket做的更好
Socket socket = serverSocket.accept();
// 创建读取的输入流线程
Thread serverInputThread = new Thread(new ServerInputRunnable(socket));
// 创建写入的输入流线程
Thread serverOutputThread = new Thread(new ServerOutputRunnable(socket));
// 启动线程 不停的读和写
serverInputThread.start();
serverOutputThread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.net.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
/**
* @author God 服务器端____向客户端写入数据的线程
*/
public class ServerOutputRunnable implements Runnable {
private Socket socket;
public ServerOutputRunnable(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
try {
OutputStream os = socket.getOutputStream();
while (true) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
String line = reader.readLine();
os.write(line.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端
package com.net.socket;
import java.net.Socket;
public class SocketClient {
public static void main(String[] args) throws Exception {
// 创建客户点socket对象 连接服务器端socket 端口一致
Socket socket = new Socket("127.0.0.1", 3000);
// 创建客户端 独立的写的线程和读的线程
Thread ClientInputThread = new Thread(new ClientInputRunnable(socket));
Thread ClientOutputThread = new Thread(new ClientOutputRunnable(socket));
ClientInputThread.start();
ClientOutputThread.start();
}
}
package com.net.socket;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class ClientInputRunnable implements Runnable {
private Socket socket;
public ClientInputRunnable(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
try {
// 获得输入流
InputStream is = socket.getInputStream();
while (true) {
byte[] bytes = new byte[1024];
int length = is.read(bytes);
System.out.println("服务器:" + new String(bytes, 0, length));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.net.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class ClientOutputRunnable implements Runnable {
private Socket socket;
public ClientOutputRunnable(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
try {
OutputStream os = socket.getOutputStream();
while (true) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
String line = reader.readLine();
os.write(line.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}