菜鸟急求帮助~
我用java写了个socket通信的客户端程序,但是有的时候给服务端发信息服务端收不到,有的时候可以收到,程序运行没有错误,请高手指点下~~
public class newYCTClient {
private static Logger logger = Logger.getLogger(newYCTClient.class.getName());
protected Socket socket;
protected InputStream in;
protected OutputStream out;
byte infoFromYCT[]= null;
public void connect(String IP,int port) {//连接客户端和服务器
try {
socket = new Socket(IP,port);
// socket.setSoTimeout(10000);
in = socket.getInputStream();
out = socket.getOutputStream();
}catch(Exception ioe) {
ioe.printStackTrace();
}
System.out.println( "客户端连接服务端 ");
}
/**
* 发送和接收报文数据
* @param str 要发送的报文数据
* @return 返回接收到的报文数据
*/
public String FileInfo(String str) {
//接收到的总的数据
String infoFromNFC = null;
//按报文种类截完后的报文数据
String newinfoFromNFC = null;
BufferedReader br;
//缓冲数据用于装接收到的数据
char[] c = new char[340];
for(int i=0;i <c.length;i++) {
c[i]=0x30;
}
//发送数据
try {
OutputStreamWriter osw;
BufferedWriter bw;
osw = new OutputStreamWriter(out);
bw = new BufferedWriter(osw);
bw.write(str);
bw.flush();
}catch(
IOException ioe) {
ioe.printStackTrace();
}
//接收信息
try {
br=new BufferedReader(new InputStreamReader(in));
br.read(c);
infoFromNFC = String.valueOf(c);
}catch(IOException ioe) {
ioe.printStackTrace();
}
this.Close();
return newinfoFromNFC;
// return infoFromNFC;
}
public int Close(){
try{
out.close();
in.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
return 0;
}
return 1;
}
}
------解决方案--------------------服务器端的代码那?怎么不贴啊?