Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7163021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:49:52+00:00 2026-05-28T13:49:52+00:00

I’m using socket to establish a streaming connection with server. By creating a socket,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:49:53+00:00Added an answer on May 28, 2026 at 1:49 pm

    The three methods you mention (PrintWriter, ObjectOutputStream and HTTPConnection) will all result in something being sent to the server, but what is actually sent will differ greatly between the various methods.

    First of all, the Java ObjectOutputStream class will encode the objects that you write to it along with information about the classes that these objects are instances of, especially class names, signatures etc. This is practically only appropriate when you are communicating with another Java application that is using an ObjectInputStream on the other end to read it. It will especially not adhere to the HTTP protocol and as such you would most likely not be able to use it at all with anything that you can use an HTTPConnection to.

    Using a HttpURLConnection will actually construct and send complete (and most importantly, valid) HTTP requests to the server you are trying to connect to. For example, if you were to execute the following code:

    URL url = new URL("http://localhost/test?cmd=handshake&version=1.0");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.connect();
    

    Then the result will be that your application connects to localhost:80 and writes something like the following: (This is a simplified request. There will be additional headers and information in the actual request.)

    GET /test?cmd=handshake&version=1.0 HTTP/1.1
    Host: localhost
    

    Finally, if you are using a PrintWriter then that will actually send pure text to the server. However, if the server is a HTTP server, which is likely since you say it responds to the HTTP connection, then it is not enough just to send the command part of the request. You will have to send the rest as well, including the GET, the HTTP version, maybe some headers, the trailing empty lines and the CRLF line endings. This can all be done using a PrintWriter.

    It could be a good idea to install a network packet analyzer such as Wireshark and use it to actually see what is sent to the server when you perform your connection using a httpconnection and then trying to mimic that by manually constructing the request.

    I would also recommend reading up on some more details regarding the HTTP protocol. A good starting point can be the Wikipedia article.

    Edit based on new information in the OP:

    There are some small inconsistencies in the updated code, some of which I can assume is copy-paste mistakes, others that I would like to point out just in case.

    The solution based on a URLConnection does not specify the “/swarm” resource anywhere. Adding that will not change whether a request is actually sent or not, or even if its valid or not, but it could affect whether you can find the request in the logs or not.

    The query string does overall look reasonable with the exception that POST is written using lowercase letters. At first I didn’t think that this would make any difference, but when trying “get” vs. “GET” against http://www.google.com over Telnet, it DOES seem to make a difference. If the web server doesn’t recognize the request as a request then it won’t be able to process it and thus it might not show up in the logs as you expect.

    I would recommend that you actually read the server response and print it to System.out so that you see if the server is reporting any particular error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.