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 8465751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:08:21+00:00 2026-06-10T15:08:21+00:00

So I wrote a simple Socket program that send message from Client to Server

  • 0

So I wrote a simple Socket program that send message from Client to Server program and wanted to know what is the proper procedure to go about testing this? Both my Client and Server machines are running on Ubuntu 12.04 and I’m remote connecting to both of them.

For my Client code when I instantiate the client socket (testSocket) do I use its IP Address and Port number or Servers IP Address and Port number?

Here is the Code for Client:

public static void main(String[] args) throws UnknownHostException, IOException 
{
    Socket testSocket = null;
    DataOutputStream os = null;
    DataInputStream is = null;

    try
    {
        testSocket = new Socket("192.168.0.104", 5932);
        os = new DataOutputStream(testSocket.getOutputStream());
        is = new DataInputStream(testSocket.getInputStream());
    }
    catch (UnknownHostException e)
    {
        System.err.println("Couldn't find Host");
    }
    catch (IOException e)
    {
        System.err.println("Couldn't get I/O connection");
    }

    if (testSocket != null && os != null && is != null)
    {
        try
        {
            os.writeBytes("Hello Server!\n");

            os.close();
            is.close();
            testSocket.close();

        }

        catch (UnknownHostException e)
        {
            System.err.println("Host not found");
        }
        catch (IOException e)
        {
            System.err.println("I/O Error");
        }
    }

}

Here is the code for Server:

public static void main(String[] args) 
{
    String line = new String() ;


    try
    {
       ServerSocket echoServer = new ServerSocket(5932);

       Socket clientSocket = echoServer.accept();

      DataInputStream  is = new DataInputStream(clientSocket.getInputStream());
      PrintStream os = new PrintStream(clientSocket.getOutputStream());

           while (true) 
           {
             line = is.readLine();
             os.println(line); 
           }
    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }

}

I’m new to Sockets and not sure what I’m supposed be seeing. I compiled both programs in terminal fine but not sure which one should I be running first or do they need to be started simultaneously?

Thanks

  • 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-06-10T15:08:22+00:00Added an answer on June 10, 2026 at 3:08 pm

    Your server is running in a infinite loop. Avoid that.
    You have to restart your computer.

    while (true) 
               {
                 line = is.readLine();
                 os.println(line); 
               }
    

    try

    while (!line.equals("Hello Server!")) 
               {
                 line = is.readLine();
                 os.println(line); 
               }
    

    Run the server first. echoServer.accept(); waits for a connection. When it gets the first connection,

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

Sidebar

Related Questions

I have a C program that do recv/send operations from/to socket using a for(;;)
Hey guys I'm trying to write a simple socket program that basically send like
Thanks for reading and answering in advance! I wrote a simple C# program that
I have a program (written in C/C++) that behaves as a socket server. I'd
I wrote this simple Java program which connects to internic server and returns the
I am implementing both server and client side of a simple file download program.
I wrote two simple programs server and a client using sockets in C++ (Linux).
I am learning socket programming in c, I wrote this simple program to accept
I'm trying to write a simple server-client program, but I have a problem: I
I wrote a simple program where a server should print data sent by multiple

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.