关于socket的问题,客户端可以向服务器端发送数据,但是收不到服务器的回发数据

craigward 2011-03-16 09:06:04
本人初学java没有多久,最近被socket通讯搞得很纠结,网上有很多socket tcp通讯的例子,试着自己练习一下,建了两个项目,Web项目使用struts2框架,通过socket和java项目通讯,完成模拟登录,但是客户端总是阻塞,如题。。。
帖些代码吧。。。
服务器端
public class Listener {
public static String userid="20071308";
public static String userpassword="123456";
public static int clientnum;

public static void main(String[] args) throws IOException{
System.out.println("Server start up....");
System.out.println("Start listening...");
String sendxml;
String receivexml;
String recode;
String username;
BufferedReader is=null;
PrintWriter os=null;
String _userid="";
String _userpassword="";
ServerSocket server=null;
Socket socket=null;
try {
server=new ServerSocket(4700);
socket =server.accept();
is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
os=new PrintWriter(socket.getOutputStream());
System.out.println(is.ready());
receivexml=is.readLine();
System.out.println(receivexml);
Document doc=null;
try {
doc = DocumentHelper.parseText(receivexml);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
_userid=doc.selectSingleNode("//userid").getText();
_userpassword=doc.selectSingleNode("//userpassword").getText();
if(_userid.equals(userid)&&_userpassword.equals(userpassword)){
recode="000";
username="Libra";
}
else
{
recode="001";
username="";
}
sendxml="<root><recode>"+recode+"</recode><username>"+username+"</username></root>";
System.out.println(sendxml);
os.print(sendxml);
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

客户端Action
public class LoginAction{
private String userid;
private String userpassword;
private String username;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUserpassword() {
return userpassword;
}
public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}


public String execute(){
String result="success";
String recode="";
String returnxml="";
Socket socket=null;
BufferedReader is=null;
PrintWriter os=null;
boolean flag=true;
String sendxml="<root><userid>"+this.userid+"</userid><userpassword>"+this.userpassword+"</userpassword></root>";
try {
socket=new Socket("127.0.0.1",4700);
is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
os=new PrintWriter(socket.getOutputStream());
os.print(sendxml);
os.flush();
//到这里就不动了,读不到东西,服务器没有发过来?
returnxml=is.readLine();
System.out.println(returnxml);
Document doc=DocumentHelper.parseText(returnxml);
recode=doc.selectSingleNode("//recode").getText();
if(recode=="001")
result="failed";
else
this.username=doc.selectSingleNode("//username").getText();


} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

}
请朋友们有时间帮我瞅瞅问题的症结在哪里吧。。。
...全文
424 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
craigward 2011-03-16
  • 打赏
  • 举报
回复
还有,刚才看个帖子说,服务器端的outputsteam不能关闭,否则服务器端的inputstream会读不到数据。。。但是我关不关都一样啊,我最后连socket的没有关。应该不是这个问题吧。。。。
craigward 2011-03-16
  • 打赏
  • 举报
回复
客户端都是用print的,服务器端不行吗?我刚才试了试还是不行啊,还有我在客户端加了一句
System.out.println(is.ready())始终是false,说明这个inputstream有问题?
uastation 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 craigward 的回复:]

那个,我好想没有40分,为什么可以悬赏40分。。。
[/Quote]
这个是有40分的~

服务器收不到客户发来的数据,os.print(sendxml);这句代码,应该要用os.wrirte()方法,你要保证数据是写进去的,然后再读出来~
craigward 2011-03-16
  • 打赏
  • 举报
回复
那个,我好想没有40分,为什么可以悬赏40分。。。

62,614

社区成员

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

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