package test;
import java.net.*;
import java.io.*;
public class Client{
private static Socket server;
private BufferedWriter out2;
private BufferedReader in;
private String IP = "xxx.xxx.x.xxx";
private String recmsg = null;
/**
* 连接控制器
*/
public void connsocket(){
try{
//server = new Socket(InetAddress.getLocalHost(),1001);
server = new Socket(IP,1001);
server.setSoTimeout(100);
in = new BufferedReader(new InputStreamReader(server.getInputStream()));
out2=new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
System.out.println("客户端启动...");
System.out.println("监听IP:"+server.getInetAddress().toString());
System.out.println("监听端口:"+server.getPort());
//向序列号为19998的控制器发送请求时间
//String str = "[B7][01][4E][1E][01][30][00][9E][B7]";
//System.out.println("gogogo!");
}catch(Exception ee){
ee.printStackTrace();
}
}
/**
*
* @param sendmsg
* @return
* @throws IOException
*/
public boolean sendMsg(String sendmsg) throws IOException{
connsocket();
out2.write(sendmsg);
out2.flush();
return true;
}
/**
*
* @return
* @throws IOException
*/
public String receiveMsg(String msg){
String str =null;
//System.out.println("开始接受");
try {
sendMsg(msg);
System.out.println("发送完毕,开始接受");
while((str=in.readLine())!=null){ //到此无论是否null都不执行了,为什么
System.out.println("=============");
recmsg +=str;
//System.out.println("重要的");
}
System.out.println("接受完毕");
//return recmsg;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("接受异常");
//return null;
}catch(Exception ee){
ee.printStackTrace();
System.out.println("其它异常");
//return null;
}
finally{
// in.close();
// out2.close();
// server.close();
}
return recmsg;
}
/**
* 测试用例
* @param args
*/
public static void main(String[] args){
Client c = new Client();
String msg = "[B7][01][4E][1E][01][30][00][9E][B7]";
//System.out.println("返回信息:"+c.receiveMsg(msg));
c.receiveMsg(msg);
System.out.println("main is going...!");
}
}不在执行了,报超时,不知道是软件还是硬件的问题,请高手指点?
import java.net.*;
import java.io.*;
public class Client{
private static Socket server;
private BufferedWriter out2;
private BufferedReader in;
private String IP = "xxx.xxx.x.xxx";
private String recmsg = null;
/**
* 连接控制器
*/
public void connsocket(){
try{
//server = new Socket(InetAddress.getLocalHost(),1001);
server = new Socket(IP,1001);
server.setSoTimeout(100);
in = new BufferedReader(new InputStreamReader(server.getInputStream()));
out2=new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
System.out.println("客户端启动...");
System.out.println("监听IP:"+server.getInetAddress().toString());
System.out.println("监听端口:"+server.getPort());
//向序列号为19998的控制器发送请求时间
//String str = "[B7][01][4E][1E][01][30][00][9E][B7]";
//System.out.println("gogogo!");
}catch(Exception ee){
ee.printStackTrace();
}
}
/**
*
* @param sendmsg
* @return
* @throws IOException
*/
public boolean sendMsg(String sendmsg) throws IOException{
connsocket();
out2.write(sendmsg);
out2.flush();
return true;
}
/**
*
* @return
* @throws IOException
*/
public String receiveMsg(String msg){
String str =null;
//System.out.println("开始接受");
try {
sendMsg(msg);
System.out.println("发送完毕,开始接受");
while((str=in.readLine())!=null){ //到此无论是否null都不执行了,为什么
System.out.println("=============");
recmsg +=str;
//System.out.println("重要的");
}
System.out.println("接受完毕");
//return recmsg;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("接受异常");
//return null;
}catch(Exception ee){
ee.printStackTrace();
System.out.println("其它异常");
//return null;
}
finally{
// in.close();
// out2.close();
// server.close();
}
return recmsg;
}
/**
* 测试用例
* @param args
*/
public static void main(String[] args){
Client c = new Client();
String msg = "[B7][01][4E][1E][01][30][00][9E][B7]";
//System.out.println("返回信息:"+c.receiveMsg(msg));
c.receiveMsg(msg);
System.out.println("main is going...!");
}
}不在执行了,报超时,不知道是软件还是硬件的问题,请高手指点?
解决方案 »
- 导出报表时,如何用el表达式做判断输出?
- spirng+hibernate新手问个spring mvc相关的问题
- Struts+Hibernate映射无主键表的问题
- 【500分】求“用jbuilder9 + weblogic开发web service的step by step教程?最好是调用ejb”急!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 高手帮忙,Tomcat+mysql的中文乱码问题!!!!!
- Weblogic的连接池应怎么设置?(菜鸟问题)
- <<J2EETutorial>>上的问题
- 如何部署RMI?——在线等!
- static public int g() 和 public static int g()两个方法有什么区别
- javax.persistence.Query 如何设置数组(集合)参数(spring data jpa)
- struts中怎样使用Validator验证
- AOM2.0 w:datagrid组件 无法显示数据
楼主截止到2008-06-18 17:16:34的汇总数据:
发帖数:24
结贴数:12
结贴率:50.00%
楼主加油