package com.xx.network;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class MyServer3 {
static String[] datas = { "死鬼", "傻逼", "高富帅", "奶奶个熊", "白富美" };
static Random random = new Random();
public static void main(String[] args) throws IOException {
// 服务器的套接字:监听客户的连接
ServerSocket serverSocket = new ServerSocket(5200);
System.out.println("程序开始了");
for (;;) {
// 获得连接请求--阻塞的
Socket accept = serverSocket.accept();
System.out.println("来了:" + accept.getInetAddress());
new MyThread(accept).start();
}
}
static class MyThread extends Thread {
Socket socket;
public MyThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
// 获得输出流
OutputStream out = socket.getOutputStream();
// 获得答复
String data = datas[random.nextInt(datas.length)];
Writer writer = new OutputStreamWriter(out);
writer.write(data);
writer.flush();
// 关闭流
writer.close();
socket.close();
} catch (Exception e) {
}
}
}
}
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class MyServer3 {
static String[] datas = { "死鬼", "傻逼", "高富帅", "奶奶个熊", "白富美" };
static Random random = new Random();
public static void main(String[] args) throws IOException {
// 服务器的套接字:监听客户的连接
ServerSocket serverSocket = new ServerSocket(5200);
System.out.println("程序开始了");
for (;;) {
// 获得连接请求--阻塞的
Socket accept = serverSocket.accept();
System.out.println("来了:" + accept.getInetAddress());
new MyThread(accept).start();
}
}
static class MyThread extends Thread {
Socket socket;
public MyThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
// 获得输出流
OutputStream out = socket.getOutputStream();
// 获得答复
String data = datas[random.nextInt(datas.length)];
Writer writer = new OutputStreamWriter(out);
writer.write(data);
writer.flush();
// 关闭流
writer.close();
socket.close();
} catch (Exception e) {
}
}
}
}