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.
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(query);
or the other one is
out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject(msg);
out.flush();
the request I'm trying to send is something like this:
String query = String.format("cmd=%s&version=%s", "handshake", "1.0");
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
String charset = "UTF-8";
String query = String.format("cmd=%s&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 + "]");
and this is what I did in socket which didn't work
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&version=1.0\n";
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(query);
according to the server log the socket connection opened and closed without getting any request