求点拨一个Socket通信程序的调试.
我在写一个CS结构的聊天程序,我用我的服务端向客户端写了一串字符:
while(true){
buffer2 = String.valueOf(Localdemo.LocalDoctoronline[0]);
for(int i = 1;i<200;i++){
buffer2 = buffer2 + "%" + String.valueOf(Localdemo.LocalDoctoronline[i]);
}
MyUserClientNum = String.valueOf(ChatServer.mapDoctoronline.get(0).Clientnum);
for(int i = 1;i<200;i++){
MyUserClientNum = MyUserClientNum + "%" + String.valueOf(ChatServer.mapDoctoronline.get(i).Clientnum);
}
System.out.println("111111111111111111111111111111111111111");
System.out.println("MyUseronlineInfo");
System.out.println("======>"+MyUserClientNum);//“拼接后的用户“并发聊天数”.
System.out.println("++++++>"+buffer2);//“拼接后的用户在线状态””.
synchronized(so2){
String str0 = "0#0#1#"+MyUserClientNum+"$"+buffer2+"#BeiJing";//向D端发出“将要在"第128行"代码处开始的逻辑中进行向D端发送数据”“请D端打开对应的线程”“准备接收数据”.int len = str.length();
so2.write(str0);
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
package s_port_package_BeiJing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class SportDSocketClient {
private Socket s;
private BufferedReader br;
private PrintWriter pw;
String MyKey = "CJCO5888CJCO";
public SportDSocketClient(String ip, int port) {
try {
s = new Socket(ip, port);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
} catch (Exception e) {
e.printStackTrace();
}
}
public String read() throws IOException {
return br.readLine();
}
public void write(String content) {
pw.println(content);
pw.flush();
}
public Socket getS() {
return s;
}
public void setS(Socket s) {
this.s = s;
}
}
while(true){
Socket so1 = sso1.nextSocket();
al19.add(so1);
try {
buffer = sso1.read(so1);
System.out.println("接收S端数据:"+buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] buffertemp = buffer.split("[#]");
condition = Integer.parseInt(buffertemp[0]);
para = Integer.parseInt(buffertemp[1]);
MyUserOnlineWorkNum = Integer.parseInt(buffertemp[2]);
SocketData = buffertemp[3];
MyServerPhysicalPlace = buffertemp[4];
switch(condition){
case 0://面向C端北京用户进行的用户在线状态推送的功能线程.
{
Complete_D_ManageTheChannel11DSRecvSportClientNumandOnline m1 = new Complete_D_ManageTheChannel11DSRecvSportClientNumandOnline("Thread_1",SocketData,MyUserRole,sso1,so1);
m1.start();
break;
}
}
}
package c_port_package;
public Complete_D_ManageTheChannel11DSRecvSportClientNumandOnline( String name,String MyData,String MyPhysicalName,DportSocketServer ss,Socket s) {
// TODO Auto-generated constructor stub
super(name);
ssoo3 = ss;
so1= s;
System.out.println("MyData:"+MyData);
buffer = MyData;
MySysName = MyPhysicalName;
}
public void run(){
System.out.println("第二步!!成功!!");
System.out.println(buffer);
String[] MyUserclientNum = null;
String[] MyUseronline = null;
System.out.println("接收==>"+buffer);
if(buffer.contains("[$]")){
System.out.println("我是否进来了..");
bufferarray = buffer.split("[$]");
}
int num = bufferarray.length;
MyUserclientNum = new String[num];
MyUseronline = new String[num];
System.out.println("=============================="+num);
System.out.println("------------->"+bufferarray[0]);
MyUserclientNum = bufferarray[0].split("[%]");//
System.out.println(MyUserclientNum);
MyUseronline = bufferarray[1].split("[%]");//s
if(MySysName.equals("WangFuZhongXiJieHeYiYuan")){
for(int i=0;i<200;i++){
D_Updata_c_port_Data.LocalBeiJingUserClientnum[i]=Integer.parseInt(MyUserclientNum[i]);
D_Updata_c_port_Data.LocalBeiJingUseronline[i]=Integer.parseInt(MyUseronline[i]);
System.out.println("成功!!");
}
}else if(MySysName.equals("CJCO")){
for(int i=0;i<200;i++){
D_Updata_c_port_Data.LocalYinChuanUserClientnum[i]=Integer.parseInt(MyUserclientNum[i]);
D_Updata_c_port_Data.LocalYinChuanUseronline[i]=Integer.parseInt(MyUseronline[i]);
System.out.println("成功!!");
}
}
}
package c_port_package;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class DportSocketServer {
private ServerSocket ss = null;
private Map<Socket, BufferedReader> rm = new HashMap<Socket, BufferedReader>();
private Map<Socket, PrintWriter> wm = new HashMap<Socket, PrintWriter>();
String MyKey = "CJCO5888CJCO";
public DportSocketServer(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public Socket nextSocket() {
Socket s = null;
try {
s = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
public String read(Socket s) throws IOException {
BufferedReader br = null;
if (null == (br = rm.get(s))) {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
rm.put(s, br);
}
return br.readLine();
}
public void write(Socket s, String content) throws IOException {
PrintWriter pw = null;
if (null == (pw = wm.get(s))) {
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
wm.put(s, pw);
}
pw.println(content);
pw.flush();
}
}