2014-07-24 195 views
0

从客户端发送字符串到服务器的正确方法是什么?java - 套接字连接,向服务器发送字符串值

try 
     { 
      ToServer.writeUTF(_textfield.getText()); 
//Try to send the strings in textarea (_textarea = new JTextArea(20,80) ) 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 

我使用writeUTF和从的readUTF服务器..

发送字符串从客户端,但它似乎并没有工作。

回答

0

由于我使用了标准的Java套接字,但我相信你会创建一个新的outputStream,将其设置为套接字的输出流,创建一个新的PrintWriter并将其设置为写入OutputStream。

OutputStream outstream = new OutputStream(socket.getOutputStream()); 
PrintWriter out = new PrintWriter(outstream); 
out.write("Hello Server!"); 
相关问题