关于socket的问题

tanwan 2010-10-26 12:11:05
A用户先socket到服务器上,然后B在socket到服务器上,(1)这里就是A先连B再次连,这时候我用B用户无限发信息给A用户都可以接到,但是这里又回到(1)处刚连上的状态用A无限发送信息给B用户这时候就出了问题...A发第1条B能收到,第2条就收不到了...后来我做了这样一个测试,把2个用户传上来的数据都在控制台打印出来都可以正常的显示,但是一做转发就出现了上述说的问题...
...全文
120 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bawgiitx 2010-10-26
  • 打赏
  • 举报
回复
看你转发部分的代码~~
zhangliang_88 2010-10-26
  • 打赏
  • 举报
回复
socket 学习了
tanwan 2010-10-26
  • 打赏
  • 举报
回复
问题解决了...
SUSHE506 2010-10-26
  • 打赏
  • 举报
回复
VB VB VB VB
tanwan 2010-10-26
  • 打赏
  • 举报
回复

class AcceptThread extends Thread
{
public void run()
{
try
{
while(true)
{
ket=server.accept();
ReadMessageThread readThread = new ReadMessageThread(ket);
readThread.start();
}
}
catch(Exception e2)
{
liaotianchuangkou.append("用户连接服务器出错\n");
}
}
}
class ReadMessageThread extends Thread
{
Socket s;
public ReadMessageThread(Socket s)
{
this.s=s;
}
public void run()
{
try
{
cin=new BufferedReader(new InputStreamReader(s.getInputStream()));
cout=new PrintStream(s.getOutputStream());
}
catch(Exception e3)
{
liaotianchuangkou.append("输入输出异常_1\n");
}
String str="";
while(true)
{
try
{
str=cin.readLine();
if(null!=str && !str.equals("")){
ipc.ipc(str.substring(0,2), str.substring(3),cout,s);
}else{
System.out.println("str对象为NULL或者是空串");
}
}catch(Exception e){
liaotianchuangkou.append("输入输出异常_2\n");
e.printStackTrace();
return ;
}
if(null==str || str.equals("QUIT"))
{
try
{
ket.close();
}
catch(Exception e1)
{
liaotianchuangkou.append("套接字关闭异常\n");
}
break;
}
}
}
}

这个是serversocket...在最了测试A和B连接上来的时候A先连,B在连,B可以无限发数据给服务器,但是A只能发一次,第2次在控制台就没反应了...
tanwan 2010-10-26
  • 打赏
  • 举报
回复

public void ipc(String systemFlag, String content, PrintStream cout, Socket s){
PrintStream pStream = null;
if("01".equals(systemFlag)){
sessionIdMap.put(content, s);
}else if("02".equals(systemFlag)){
cout.println("021" + content);
}else if("03".equals(systemFlag)){
String str = "";
str = systemFlag + "1" + content;
String receive = content.substring(10,20);
Socket nowKet = sessionIdMap.get(receive);
if(null != nowKet){
try {
pStream = new PrintStream(nowKet.getOutputStream());
pStream.println(str);
} catch (IOException e) {
System.out.println("发送错误...");
}
}else{
System.out.println("没有找到合适的对象发送消息...");
}
String returnStr = "021" + content.substring(0,10) + receive + content.substring(20);
System.out.println("回发..." + returnStr);
try{
PrintStream replyStream = new PrintStream(((Socket)sessionIdMap.get(content.substring(0,10))).getOutputStream());
replyStream.println(returnStr);
}catch(IOException e5){
System.out.println("回发出错...");
}
}else{
System.out.println("其他通道,其他...");
cout.println(content);
}
}

sessionIdMap里存放是已连接上的socket Map<String,Socket>
tanwan 2010-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bawgiitx 的回复:]
看你转发部分的代码~~
[/Quote]
大家帮忙看看...

81,122

社区成员

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

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