网络编程--基于java socket通信,实现多个客户端连接同一服务端

时间:2022-08-25 22:16:27

1 简介
刚学习java没有多久,对于socket的理解还不够,关于socket的理解自行查阅,本部分代码基本实现服务端与客户端的有效应答,关于聊天界面部分,后期打算用jsp进行处理,以及本部分的代码的完善,希望能与大家一起学习。
2 开发环境
2.1 系统:windows7
2.2 集成工具:Intellij IDEA 2016 2.2
3 主体
3.1 服务端代码
`package com.lkl.wechat.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
* Created by ALKL on 2016/8/31.
*/
public class Server {
//多个socket保存在socketlist中
// public static List socketList = Collections.synchronizedList(new ArrayList<>());
public static void main(String[] args) throws IOException {
int port = 9000;
ServerSocket ss = new ServerSocket(port);
System.out.println(“等待客户端连接…..”);
while (true)
{
BufferedReader out = new BufferedReader(new InputStreamReader(System.in));
String line = null;
Socket s = ss.accept();
//当接收到一个socket,就开启一个线程为其服务
new Thread(new ServerThread(s)).start();

        PrintStream ps = new PrintStream(s.getOutputStream());

while((line=out.readLine())!=null){
System.out.println("To Client[port:" + s.getPort() + "] 回复客户端的消息发送成功"+ line );
ps.println(line);
ps.println("lkl");
}
}
}

}
package com.lkl.wechat.server;

import java.io.*;
import java.net.Socket;

/**
* Created by ALKL on 2016/8/31.
*/
public class ServerThread implements Runnable
{
Socket s = null;
public ServerThread(Socket s) throws IOException
{
this.s=s;
}
public void run ()
{

        try {
//读取客户端发送过来的信息,并转成UTF-8的形式
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
//将发送过来的信息进行筛选
StringBuilder sb = new StringBuilder();
String temp;
int index;

//indexOf返回指定子字符串在此字符串中第一次出现处的索引
while ((temp = br.readLine()) != null) {
if ((index = temp.indexOf("lkl")) != -1) { // 遇到lkl时就结束接收
sb.append(temp.substring(0, index));
break;
}
sb.append(temp);
}
//打印输出客户端发送过来的消息
System.out.println("From Client[port:" + s.getPort()
+ "] 消息内容:" + sb.toString());
} catch (IOException e) {
System.out.println("socket断开连接,请重新运行一次");
}
}

}`
3.2 客户端代码

package com.lkl.wechat.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

/**
* Created by ALKL on 2016/8/31.
*/

public class Client {
public static void main(String [] args) throws IOException {
Socket s = new Socket("172.24.252.1",9000);
new Thread(new ClientThread(s)).start();
PrintStream ps = new PrintStream(s.getOutputStream());
String line = null;

//通过键盘输入
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while((line=br.readLine())!=null){
System.out.println("To Server[port:" + s.getPort() + "] 回复服务端的消息发送成功"+ line );
ps.println(line); //输出到客户端上面的信息
ps.println("lkl");
}
}
}
package com.lkl.wechat.client;

import java.io.*;
import java.net.Socket;

/**
* Created by ALKL on 2016/8/31.
*/

public class ClientThread implements Runnable {

Socket s = null;
public ClientThread(Socket s) throws IOException {
this.s = s;
}
@Override
public void run() {

try {
//开启线程
//从服务端读取的数据存储在缓冲区bj中
BufferedReader bj = null;
bj = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String temp;
String line = null;
int index;
//根据服务端传回来的数据,通过键盘输入相应的内容

while ((temp = bj.readLine()) != null) {
if ((index = temp.indexOf("lkl")) != -1) { // 遇到lkl时就结束接收
sb.append(temp.substring(0, index));
break;
}
sb.append(temp);
}

System.out.println("From Server[port:"+s.getPort()+"] 消息内容:"+ sb.toString());
//发送给服务端的程序
} catch (IOException e) {
e.printStackTrace();
}
}
}

4 运行结果
网络编程--基于java socket通信,实现多个客户端连接同一服务端

网络编程--基于java socket通信,实现多个客户端连接同一服务端