关于socket的问题,客户端可以向服务器端发送数据,但是收不到服务器的回发数据
本人初学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;
}
}
请朋友们有时间帮我瞅瞅问题的症结在哪里吧。。。