请教一个Swing的Socket通信的技术问题.

My_Struggle 2014-04-22 06:35:42
我写了一个Swing的IM,其中有着一块功能:

这是一个“用户兑换授权码”的系统功能。
现在画红色方框的位置出现了问题:
用户在C端点击“提交”按钮孩子后,出现了:
我掐表:“110秒之后”,C端才能够接收到D端服务器的值.
我的D端写出数据的现场代码如下:
if(mytaskstate == 1){
System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
D_port_Calc_The_CustomerSysFuctionUserRightsCode cjco = new D_port_Calc_The_CustomerSysFuctionUserRightsCode();
String MyCustomerServiceOrdernum = cjco.getMyUserRights(MyUserRights);
SocketClient so = new SocketClient(MyObjIP,5555);
String mycustomerordernum = "6丗6丗"+MyCustomerServiceOrdernum+"丠"+"Cus"+"丠"+"99999999"+"丠"+"IneedOrders丮false" +"丗"+"D";
so.writeStr(mycustomerordernum, "ManageOurAuthorizeRightsConfirmCode");
so.getMyResourceBack();
System.out.println("OKay!!The Data I Out put is:"+mycustomerordernum);
}

我的C端的接收服务器的数据的代码如下:
while(true){
Socket s1 = soo1.nextSocket();
IP = s1.getInetAddress();
MyObjIP = IP.toString().substring(1);
synchronized(s1){
try {
buffertemp3 = soo1.read(s1);
} catch (IOException e) {
// TODO Auto-generated catch block
new NoteUserSysIsInDefending();
}
}
//////"D-C传输一笔数据");
// //////////"C端接收到S端的数据!!D-C端系统信道启动!!");
// ////buffertemp3);
//////"C端接收到的数据:buffertemp3----->"+buffertemp3);
System.out.println("GetDportSocket类接收到的服务器的传输数据" + buffertemp3);
String[] bufferData3 = buffertemp3.split("丗");
System.out.println("GetDportSocket类接收服务器传输数据的字符串数组"+bufferData3);
sign3 = Integer.parseInt(bufferData3[0]);
System.out.println("GetDportSocket类接收到的数据第一字段:sign3----->"+sign3);
para3 = Integer.parseInt(bufferData3[1]);
System.out.println("GetDportSocket类接收到的数据第二字段:para3----->"+para3);
SocketInfo = bufferData3[2];
System.out.println("GetDportSocket类接收到的第三字段:SocketInfo----->"+SocketInfo);
MyUserRole = bufferData3[3];
System.out.println("GetDportSocket类接收到的第四字段:MyUserRole----->"+MyUserRole);
MyObjIP = bufferData3[4];
System.out.println("GetDportSocket类接收到的第五字段:MyObjIP----->"+MyObjIP);
MyThreadname = bufferData3[5];
System.out.println("GetDportSocket类接收到的D端的线程:MyThread----->"+MyThreadname);

现在从console窗口中打印的数据来看:
D端已经接收并且发出了自己在图片中的“回写数据”,但是,现在小弟遇到的场景是:
C端在小弟多次掐表到110秒后,才打印出我想要的数据:
D端服务器的回写数据.
我用的“SocketServer”与“SocketClient”两个Socket API,是经过了我数千次测试成功的绝对好用的武器.
这里面他们肯定能够担负起在网络中读写数据的任务.
但是,现在我遇到的问题是:
S端回写的数据,我第一时间接收到了,可是D端服务器的回写数据,我经过多次测试,总是在写出数据后110左右的时间点上C端才能够接受到数据.
我是采用“本地测试”环境进行的上述测试的进行的.
希望高手,组织能够指导:
我的程序,什么地方有问题,导致了“我的程序的这个让用户抓墙”的故障?
希望能够得到CSDN总部版主,路过的开发者,还有广大的社区牛人的解答.
我把我的API贴到下面:
package c_port_packageBJTeacher;
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 SocketServer {
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";
BufferedReader br = null;
PrintWriter pw = null;

public SocketServer(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 {
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 {
if (null == (pw = wm.get(s))) {
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
wm.put(s, pw);
}
pw.println(content);
pw.flush();
}

public void getMyResourceBack(){
if(pw!=null){
pw.close();
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rm!=null){
rm.clear();
}if(wm!=null){
wm.clear();
}if(ss!=null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

package c_port_packageBJTeacher;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketClient {

public Socket getS() {
return s;
}

public void setS(Socket s) {
this.s = s;
}

private Socket s;
private InputStream in;
private OutputStream out;
private BufferedInputStream inByte;
private OutputStream outByte;
private BufferedReader inStr;
private PrintWriter outStr;

private long size = 0;

public SocketClient(String ip, int port) {
try {
s = new Socket(ip, port);

in = s.getInputStream();
out = s.getOutputStream();

inByte = new BufferedInputStream(in);
outByte = out;

inStr = new BufferedReader(new InputStreamReader(in));
outStr = new PrintWriter(new OutputStreamWriter(out));
} catch (Exception e) {
e.printStackTrace();
}
}

public String readStr() throws IOException {
synchronized (this.in) {
return this.inStr.readLine();
}
}

public void writeStr(String content,String MyThreadname) {
synchronized (this.out) {
String IP = GetMyUserIP.getMyIP();
outStr.println(content+"丗"+IP+"丗"+MyThreadname);
outStr.flush();
}
}

public File readToFile(File file) throws IOException {
synchronized (this.in) {
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024 * 8];
int count = 0;
while (-1 != (count = this.inByte.read(temp))) {
fos.write(temp, 0, count);
fos.flush();
}
fos.close();
return file;
}
}

public void writeFile(File file) {
synchronized (this.out) {
size = file.length();
this.noticeFileSize(size);

FileInputStream fis;
try {
fis = new FileInputStream(file);
byte[] temp = new byte[1024 * 8];
int count = 0;
while (-1 != (count = fis.read(temp))) {
this.outByte.write(temp, 0, count);
this.outByte.flush();
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}

// long progress = 0;
// while (size != (progress = getServerReceiveSize())) {
// //////System.out.println("progress : " + (progress / (size / 100)) + "%");
// }
}
}

private void noticeFileSize(long l) {
String str = l + "";
int j = 19 - str.length();
for (int i = 0; i < j; i++) {
str = "0" + str;
}
this.writeByByte(str);
}

protected void writeByByte(String content) {
synchronized (this.out) {
try {
this.out.write(content.getBytes());
this.out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}

private long getServerReceiveSize() {
synchronized (in) {
byte[] b = new byte[19];
try {
this.in.read(b);
} catch (IOException e) {
e.printStackTrace();
}

return Long.parseLong(new String(b));
}
}

public String getProgress() {
long l = this.getServerReceiveSize() / (size / 100);
if(100 == l) {
return null;
}
return l + " %";
}

public void getMyResourceBack(){
if(inStr!=null){
try {
inStr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(outStr!=null){
outStr.close();
}
if(s!=null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

希望得到高手的指导:
助我:成·佛!!!!
谢谢观世音大仕!!谢谢地藏王菩萨!!谢谢文殊菩萨与我最最最最崇拜的:释·迦·牟·尼!!
...全文
281 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
My_Struggle 2014-04-23
  • 打赏
  • 举报
回复
谢谢无冕帝王CSDN观世音大仕的再次亲临指导!! 谢谢neustar1老师的相助!!!! 真的,小弟如果成佛了,一定努力对得起: 斗·战·胜·佛·这四个字. 努力建设我们,伟大的祖国!!!!!!!! 离不开CSDN中的每一位好朋友: withiter版主,方名师姐,(CSDN的观世音大仕),一再上线远程为学生我做讲解的CSDN地藏王菩萨levelmini好哥哥,还有学生创建的群里的一帮好兄弟的相助!!!! 深深滴做一句感慨: 要成佛的感觉,真是“太美妙了”!!!!!!!
__cc__ 2014-04-22
  • 打赏
  • 举报
回复
回看了下代码,你客户端貌似发送了换行符。你把C端的代码执行过程中打印时间点,看下这个延迟位于哪个位置。 Socket s1 = soo1.nextSocket(); IP = s1.getInetAddress(); MyObjIP = IP.toString().substring(1); synchronized(s1){ try { buffertemp3 = soo1.read(s1); } catch (IOException e) { // TODO Auto-generated catch block new NoteUserSysIsInDefending(); }
__cc__ 2014-04-22
  • 打赏
  • 举报
回复
虽然你说你的SocketServer经得起考验,但是我还是怀疑这个地方: public String read(Socket s) throws IOException { if (null == (br = rm.get(s))) { br = new BufferedReader(new InputStreamReader(s.getInputStream())); rm.put(s, br); } return br.readLine(); } 参考: public String readLine() throws IOException Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. 这就好比控制台输入一样,我要是不敲回车是不会返回的,而你110s左右返回了,应该是连接断开导致返回的缘故。 建议使用read方法直接读取。
  • 打赏
  • 举报
回复
有没有debug,具体在那一段代码块停留110s呢?
My_Struggle 2014-04-22
  • 打赏
  • 举报
回复
诛路神佛,小弟补充一句: 小弟的IP地址与端口号都是“经过了无数回检查”“没有问题”的。 并且,小弟的这个故障,已经“折腾了小弟72个小时”了...... 希望得到组织,得到高手的赐教!!!! 谢谢CSDN!! 谢高手!!!!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧