编写一服务器端程序,实现读取客户端发送过来的一组整数,表现为一组数与数之间用空格隔开的字符串。对这组整数进行排序处理后,返回相应的字符串给客户端,如果数据格式不正确,则返回错误信息,以本机作为服务器。
public class Server {
public static void main(String[] args) {
BufferedReader br = null;
String clientStr="";
try {
//创建一个监听的端口
ServerSocket ss = new ServerSocket(3838);
Socket s = null;
System.out.println("服务器启动...");
//接受客户端通信请求,并建立专用通信端口
s = ss.accept();
System.out.println("有客户端发送请求");
Send send = new Send(s);
Thread t1 = new Thread(send);
t1.start();
while(true){
//获取客户端通信的信息
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
clientStr = br.readLine();
System.out.println("客户端:"+clientStr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Client {
public static void main(String[] args) {
BufferedReader br = null;
String clientStr="";
try {
//建立通信端口,并向服务器端发送通信请求
Socket s = new Socket("127.0.0.1",3838);
System.out.println("与服务器端建立连接");
Send send = new Send(s);
Thread t1 = new Thread(send);
t1.start();
while(true){
//获取客户端通信的信息
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
clientStr = br.readLine();
System.out.println("服务器端说:"+clientStr);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Send implements Runnable{
Socket s ;
Scanner in = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public Send(Socket s){
this.s = s;
}
public void run() {
// TODO Auto-generated method stub
PrintWriter out = null;
try {
while(true){
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
String str = in.next();
//br.readLine();
out.println(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}
}
}
编写一个简单的socket通讯程序:
1)、客户机程序,从控制台输入字符串,发送到服务器端,并将服务器返回的信息显示出来。
2)、服务器端程序,从客户机接收数据并打印,同时将从标准输入获取的信息发送给客户机
3)、满足一个服务器可以服务多个客户
实现效果如图:
图1:服务器端效果:
图2:客户器端效果:
public class Server {
static List l = new ArrayList();
public static void main(String[] args) {
try {
ServerSocket so =new ServerSocket(4001);
System.out.println("服务器启动...");
while(true){
Socket s = so.accept();
System.out.println(s.getInetAddress()+"发送请求");
l.add(s);
One o = new One();
Thread t = new Thread(o);
t.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class One implements Runnable {
public void run() {
Socket s = Server.l.get(Server.l.size() - 1);
String str = "";
SendAll sendAll = new SendAll();
Thread t1 = new Thread(sendAll);
t1.start();
try {
while (true) {
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
str = br.readLine();
System.out.println("客户端" + s.getInetAddress() + "说:" + str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(s.getInetAddress() + "用户下线");
Server.l.remove(s);
}
}
}
public class SendAll implements Runnable{
Scanner in = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public void run(){
PrintWriter out = null;
try {
while(true){
String str =br.readLine();
for(Socket s:Server.l){
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
out.println(str);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}
}
}
public class Client {
public static void main(String[] args) {
BufferedReader br = null;
String clientStr="";
try {
//建立通信端口,并向服务器端发送通信请求
Socket s = new Socket("127.0.0.1",4001);
System.out.println("与服务器端建立连接");
Send send = new Send(s);
Thread t1 = new Thread(send);
t1.start();
while(true){
//获取客户端通信的信息
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
clientStr = br.readLine();
System.out.println("服务器端说:"+clientStr);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Send implements Runnable{
Socket s ;
Scanner in = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public Send(Socket s){
this.s = s;
}
public void run() {
// TODO Auto-generated method stub
PrintWriter out = null;
try {
while(true){
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
String str =br.readLine();
out.println(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}
}
}