server端
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
*learning socket code is from the website:
* <p>
* https://blog.csdn.net/qq_27525611/article/details/102633014
* <p>
* @author Mengxin Song
*/
public class Server {
// there is another java class named Server_Thread
public static void main(String[] args) throws Exception {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Welcome to my server\n" +
"my server is waiting for Clients");
// using while to keep accept client
int i = 1;
String ip = (InetAddress.getLocalHost().getHostAddress());
System.out.println("The ip address is :"+ip);
while (true) {
// server accept the client connection request
Socket serverClient = serverSocket.accept();
System.out.println(" Connected to the Server Successfully!\n"+ip+" says");
// send the request to a separate thread and create a new one
new Server_Thread(serverClient).start();
System.out.println("this is the "+i+" require");
// i++ too show that how many number of clint there
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
serverThread类
重写了thread 的 run方法
最后返回一些quote给client证明返回成功
import java.net.Socket;
/**
*learning from :https://www.cnblogs.com/zhuchengyi/articles/5614660.html
*/
public class Server_Thread extends Thread{
static String hello = "From Server: Hello world";
Socket socket;
public Server_Thread(Socket s){
socket = s;
}
@Override
public void run() {
try {
String ip = (InetAddress.getLocalHost().getHostAddress());
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String quote = pick_quote();
// output the quote
bufferedWriter.write(ip+" says : "+quote);
bufferedWriter.flush();
//Refresh the buffer stream
bufferedWriter.close();
}catch(Exception e){
System.out.println(e);
}
}
private String pick_quote(){
int math = (int)(Math.random()*10);
// randomly pick one quote
String quote = "If Winter comes, can Spring be far behind ?";
if(math<3){
quote = "If Winter comes, can Spring be far behind ?";
}else if (math<6){
quote = "Keep calm and carry on!";
}else{
quote = "For my life too short for waiting";
}
String Return = "\nHello!this is my server\n" +"There is a quote for you.)\n"+quote;
return Return;
}
}
最后是client的代码我写了一些循环连接测试多线程(但是好像失败了因为代码处理的很快我不知道thread.sleep是否有效)仅供参考
import java.net.UnknownHostException;
// learning client from https://blog.csdn.net/qq_27525611/article/details/102633014
public class Client {
public static String ip;
public static int port;
public Client() {
try {
Socket socket = new Socket(ip,port);
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String message;
while((message = bufferedReader.readLine()) != null) {
System.out.println(message);
}
socket.close();
} catch (UnknownHostException var5) {
var5.printStackTrace();
} catch (IOException var6) {
var6.printStackTrace();
}
}
public static void main(String[] args) {
String serverName = args[0];
int port = Integer.parseInt(args[1]);
for (int i = 0; i < 10; i++) {
new Client();
}
}
}