Reputation: 349
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);
}
}
Upvotes: 0
Views: 8089
Reputation: 6572
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)
Upvotes: 2