求助java中Socket问题,急。。。。!
和另一台机子做数据交换。代码如下
public String send(String SendMessage,String clientIP,int clientPort) throws UnknownHostException, IOException{ System.out.println("------------clientIP="+clientIP+"-------------clientPort="+clientPort); Socket s = new Socket(clientIP,clientPort); System.out.println("s.getOutputStream()"+s.getOutputStream()); PrintStream out = new PrintStream(s.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); InputStream is = s.getInputStream(); System.out.println("SendMessage:"+SendMessage); out.print(SendMessage); int b = 0; while ((b = is.read()) != -1) { System.out.println(b); bos.write(b); System.out.println(b); } byte[] bytes = bos.toByteArray();*/ int len = -1; while((len=is.available()) <=8) { len = is.available(); System.out.println("len:"+len); } byte[] bytes = new byte[8192]; is.read(bytes, 0 , bytes.length); String ReceiveMessage = StringUtil.trim(new String( bytes)); System.out.println("ReceiveMessage:"+ReceiveMessage); out.close(); s.close(); is.close(); return ReceiveMessage; }