|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
用winrar打包j2ee的程序和用IDE打包应用程序是一样的。按照你的想法,你是不是也希望服务器都整合由一家公司提供呢?成绩
要做成如许的:
客户端会见服务器ip和port,服务器即刻发送welcome动静,然后客户端依据welcome动静判别是不是是想要毗连的服务器和端口,
假如是则入手下手发送数据,数据包含入手下手和停止的校验。服务器端发送welcome动静后入手下手吸收数据,直到停止。蛮复杂的事TMD我怎样就弄不定。
CLIENT:
packagecom.ces.mytest.socket;
importjava.net.*;importjava.io.*;
publicclassClientThreadextendsThread{privateStringipStr="";privateintport;publicClientThread(StringipStr,intport){this.ipStr=ipStr;this.port=port;}
publicvoidrun(){try{Socketclient=newSocket(ipStr,port);BufferedReaderread=newBufferedReader(newInputStreamReader(client.getInputStream()));BufferedWriterwrite=newBufferedWriter(newOutputStreamWriter(client.getOutputStream()));
if("接待".equals(read.readLine())){System.out.println("校验准确");
write.write("入手下手");write.newLine();write.write("数据");write.newLine();write.write("停止");write.newLine();write.flush();
System.out.println("发送终了");}
write.close();read.close();client.close();}catch(UnknownHostExceptionex){ex.printStackTrace();}catch(IOExceptionex){ex.printStackTrace();}}
publicstaticvoidmain(String[]args){Threadclient=newClientThread("127.0.0.1",10020);client.start();}
}
SERVER:
packagecom.ces.mytest.socket;
importjava.net.*;importjava.io.*;
publicclassServerThreadextendsThread{privateintport;publicServerThread(intport){this.port=port;}
privatebooleanstop=false;
publicvoidrun(){try{ServerSocketserver=newServerSocket(port);while(!stop){Socketclient=server.accept();BufferedReaderread=newBufferedReader(newInputStreamReader(client.getInputStream()));BufferedWriterwrite=newBufferedWriter(newOutputStreamWriter(client.getOutputStream()));System.out.println("发送接待信息");write.write("接待");write.flush();
//StringlineStr=read.readLine();//if("入手下手".equals(lineStr)){//System.out.println("入手下手承受数据!");////while(true){//lineStr=read.readLine();//if("停止".equals(lineStr)){//break;//}else{//System.out.println("数据是"+lineStr);//}//}//}
write.close();read.close();client.close();}
}catch(IOExceptionex){ex.printStackTrace();}}
publicstaticvoidmain(String[]args){Threadserver=newServerThread(10020);server.start();}
publicbooleanisStop(){returnstop;}
publicvoidsetStop(booleanstop){this.stop=stop;}}
只需我将ServerThread类的StringlineStr=read.readLine();的正文往失落,程序一定出成绩,甚么成绩呢?剖析发明ServerThread的Output流必需等write.close()实行完才发送,而read.readLine()程序就会停在那边,所谓的block程序,可参考上明显说write.flush()便可发送啊,弄不懂弄不懂,如许我的握手协定怎样做啊??
手机用到的是用j2me所编出来的小程序。 |
|