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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:30:19+00:00 2026-06-15T14:30:19+00:00

i have created a server and client application which works. however the client or

  • 0

i have created a server and client application which works. however the client or server isn’t working right. if you run the server and run the client on the same machine it works. but if u run the server on 1 machine and run the client on another machine it doesn’t work. if you could help me spot why it isn’t allowing the connection or how to establish connection between the two computers that will be great. thanks you loads. the code is below:

server-----------------------------------------------------------------

import java.io.*;
import java.net.*;

import javax.swing.*;

public class AdditionalServer extends JFrame {
    private JTextArea textWindow= new JTextArea();
    private int port;

    // the constructor
    public AdditionalServer(int portIn)
    {
        port = portIn;
        setTitle("Addition Sever");
        add("Center",textWindow);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,300);
        setVisible(true);
        startServer();
    }

    private void startServer() {
        // TODO Auto-generated method stub
        // declare a "general" socket and a server socket
        Socket connection;
        ServerSocket listenSocket;
        //declare low level and high level objects for input
        InputStream inStream;
        DataInputStream inDataStream;

        // declare low level and high level objects for output
        OutputStream outStream;
        DataOutputStream outDataStream;

        // declare other variables
        String client;

        boolean connected;

        while(true){
            try{
                // create a server socket
                listenSocket= new ServerSocket(port,0, InetAddress.getLocalHost());
            //  listenSocket= new ServerSocket(port);
                textWindow.append("Listening on port "+ port +"\n");

                //listen for a connection from the client 
                connection =listenSocket.accept();
                connected = true;

                // create an input stream from the client
                inStream = connection.getInputStream();
                inDataStream = new DataInputStream(inStream);

                // create an output stream from the client
                outStream = connection.getOutputStream();
                outDataStream = new DataOutputStream(outStream);

                // wait for a string from the client
                client = inDataStream.readUTF();
                textWindow.append("Connection esablished with "+ client+ "\n");

                int first, second,sum1;
                String sum = "hi";
                while(connected){
                    //read an integer from the client
                    first = inDataStream.readInt();
                    textWindow.append("First number receievd: "+ first + "\n");

                    //read an integer from the client
                    second = inDataStream.readInt();
                    textWindow.append("Second number receievd: "+ second + "\n");

                    sum1 = first + second;
                    textWindow.append("Sum returned: " + sum1 +"\n");

                    // send the sum to the client 
                    outDataStream.writeInt(sum1);
                    //outDataStream.writeUTF("hi");
                }
            }catch(IOException e){
                connected = false;
            }
        }
    }

    public static void main (String args []){
        new AdditionalServer(8900);

    }
}

client----------------------------------------------------------------------
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class AdditionClient extends JFrame implements ActionListener {

    // declare the visual components

    private JTextField firstNumber = new JTextField(3);
    private JLabel plus = new JLabel("+");
    private JTextField secondNumber = new JTextField(3);
    private JLabel equals = new JLabel("=");
    private JLabel sum= new JLabel();
    private JTextField msg = new JTextField(20);
    private JButton addButton= new JButton("Press to see the sum of the two numbers");

    // declare low level and high level objects for input
    private InputStream inStream;
    private DataInputStream inDataStream;

    // declare low level and high level objects for output
    private OutputStream outStream;
    private DataOutputStream outDataStream;

    // declare socket
    private Socket connection;

    // declare attribute to told details of remote machine and port
    private String remoteMachine;
    private int port;

    // constructor

    public AdditionClient(String remoteMachineIn, int portIn){
        remoteMachine = remoteMachineIn;
        port= portIn;

        //add the visual components
        add(firstNumber);
        add(plus);
        add(secondNumber);
        add(equals);
        add(sum);
        add(msg);
        add(addButton);

        // configure the frame
        setLayout(new FlowLayout());
        setTitle("Addtion Client");
        msg.setHorizontalAlignment(JLabel.CENTER);
        addButton.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,150);
        setVisible(true);

        //start the helper method that starts the client
        startClient();
    }

    private void startClient() {
        // TODO Auto-generated method stub
        try{
            // attempt to create a connection to the server
            connection = new Socket(remoteMachine,port);
            msg.setText("connection establish");

            // create an input stream from the server
            inStream = connection.getInputStream();
            inDataStream = new DataInputStream(inStream);

            //create output stream to the server
            outStream = connection.getOutputStream();
            outDataStream = new DataOutputStream(outStream);

            //send the host IP to the server
            outDataStream.writeUTF(connection.getLocalAddress().getHostAddress());

        }catch (UnknownHostException e){
            msg.setText("Unknow host");
        }
        catch (IOException except){
            msg.setText("Network Exception");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        try{
            // send the two integers to the server
            outDataStream.writeInt(Integer.parseInt(firstNumber.getText()));
            outDataStream.writeInt(Integer.parseInt(secondNumber.getText()));

            //read and display the results sent back from the server
            //String results= inDataStream.readUTF();
            int results= inDataStream.readInt();
            sum.setText(""+results);
        }catch(IOException ie){
            ie.printStackTrace();
        }
    }

    public static void main (String args[]){
        new AdditionClient("192.168.07", 8900);
    }
}
  • 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-15T14:30:20+00:00Added an answer on June 15, 2026 at 2:30 pm

    I think your problem is here.

    listenSocket= new ServerSocket(port,0, InetAddress.getLocalHost());
    

    InetAddress.getLocalHost() should be your bind ip. Provide your machine ip here and which should also be visible to your client. I would suggest you to send a ping request from your client machine.

    Try to do a ping "your bind ip" to check that your server machine is reachable over the network.

    Or try by giving port only.

    listenSocket= new ServerSocket(port)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a server in java which accepts client connections. But I am
I have created a TCP server-client application in java which exchange periodic messages between
I have created a Client/Server application with the IdTCPServer component. The clients connect and
I want to create an application which will have a client and server components.
I have created a server/client application. Both of them are written in C#. It
I have written a TCP server implementation using which I created an application which
I have created a CA, server and client certificates with OpenSSL for my application.
I am working on a mini-project which consists of doing a client-server chat application.
I have an application in which a client component is run in a managed
I have created my web socket server and client using this simple tutorial here

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.