title: sending request to server using socket in java
body: <p>I'm using socket to establish a streaming connection with server. By creating a socket, I tried to send a request to the server using the socket. but the request isn't received by server no matter which method I use. these are the methods I used to send request.</p>
  
  <pre><code>PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
  out.println(query);
  </code></pre>
  
  <p>or the other one is </p>
  
  <pre><code> out = new ObjectOutputStream(connection.getOutputStream());
   out.writeObject(msg);
   out.flush();
  </code></pre>
  
  <p>the request I'm trying to send is something like this:</p>
  
  <pre><code>String query = String.format("cmd=%s&amp;version=%s", "handshake", "1.0");
  </code></pre>
  
  <p>I know that sending this query is much easier by using Httpconnection, but this is the way that it has to be done. so please help me with this.
  this is the httpconnection commands</p>
  
  <pre><code> String charset = "UTF-8";
   String query = String.format("cmd=%s&amp;version=%s", "handshake", "1.0");
   String charset = "UTF-8";
   URL url = new URL("serveraddress:8080");           
   URLConnection conn = url.openConnection();         
   conn.setDoOutput(true);            
   conn.connect();            
   OutputStream output = conn.getOutputStream();              
   output.write(command.getBytes(charset));                                       
  
   InputStreamReader content = new InputStreamReader(conn.getInputStream());                              
   String result ="";
   int c = content.read();
   while (c != -1) {
      result = result + ((char) c);  
          c = content.read();
  } 
   Log.v(logTag, "Received: [" + result + "]");
  </code></pre>
  
  <p>and this is what I did in socket which didn't work</p>
  
  <pre><code> URL url = new URL("serveraddress:8080");               
   String server_name = url.getHost();
   int port = url.getPort();
   Socket socket = new Socket(server_name, port);                         
  
   String query = "post /swarm HTTP/1.1\nHost: serveraddress\n" +
   "content-length: 27\nkeep-alive: 300\nconnection: keep-alive\n\n" +
   "cmd=handshake&amp;version=1.0\n"; 
  
   PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
   out.println(query);
  </code></pre>
  
  <p>according to the server log the socket connection opened and closed without getting any request</p>