📄 server.java
字号:
package com.test;
import java.io.*;
import java.net.*;
import java.util.*;
public class server
{
public static void main(String args[])
{
ServerSocket ss = null;
Server_thread st;
Socket you = null;
//监听客户端,建立与之连接的线程
while(true)
{
try
{
ss = new ServerSocket(4331);
}catch(IOException e){System.out.println("正在监听"+"Error:"+e);}
try
{
you = ss.accept();
}catch(IOException e){System.out.println("正在等待客户"+e);}
if(you!=null)
{
new Server_thread(you).start();
}
else
{
continue;
}
}
}
static class Server_thread extends Thread
{
Socket socket;
DataOutputStream out = null;
DataInputStream in = null;
String s = null;
Server_thread(Socket t)
{
socket = t;
try
{
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}catch(IOException e){}
}
//处理客户端传来的信息,并回应客户端
public void run()
{
while(true)
{
try
{
s = in.readUTF();
}catch(IOException e){System.out.println("ERROR:"+e);}
try
{
if(s.equals("bye"))
{
out.writeUTF(s);
socket.close();
}
else
{
out.writeUTF("我是服务器,你对我说:"+s);
}
}catch(IOException e){}
}
}
};
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -