Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem connecting with sockets

Status
Not open for further replies.

dexter195

Programmer
Jun 18, 2003
220
EU
im creating a program to send text from one gui to another using sockets. ive got the server side program and the client side program. i can do this when i use the dos screens but when i try to do this using the gui i keep getting the error
"Connection refused: connect"
At the moment the server can only receive text and the client can only send it.

this is how ive set up the sockets in the server side program. am i declaring the sockets in the right place? ive tried to set it up where i set up the labels and text boxes etc. but that didnt work and i also tried to set the sockets up in the area where you add the components of the gui but that didnt work either.

Server side

public void actionPerformed(ActionEvent e)
{
Object target = e.getSource();

String senderText = "";
String receivedText = "";

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));


try
{
//Setting up the sockets for the server to comunicate to the client
Socket client;
ServerSocket server = new ServerSocket(2000);

client = server.accept();
//setting up the input streams so the server can receive text
InputStream inputFromClient = client.getInputStream();
BufferedReader fromClient = new BufferedReader(new InputStreamReader(inputFromClient));

String clientInput;

clientInput = fromClient.readLine();
taReceived.append("Received=> " + clientInput + "\n");

}//end try

catch (IOException except)
{
//taReceive is a 'text area' to show the received text
taReceived.append(except.getMessage() + "\n");
}



}


Client Side

public void actionPerformed(ActionEvent e)
{
Object target = e.getSource();

String senderText = "";
String receivedText = "";

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));


try
{
InetAddress inet = InetAddress.getByName("localhost");
try
{
//setting up the inet address and socket number to use
Socket server1 = new Socket(inet, 2000);

//setting up the outputs streams so text can be sent to the
//server
OutputStream o = server1.getOutputStream();
PrintWriter p = new PrintWriter(o);

if (target == bSend)
{
//senderText is a string that stores the text thats to be sent
senderText = taSender.getText();

if(senderText.equals(""))
{
taReceived.append("Please enter some text.\n");
}

else if (senderText.equals("over"))
{
taReceived.append("Do you wish to close the program?\n");

String ans = taSender.getText();

if (ans.equals("yes"))
{
System.exit(0);
}
}//end else if

else
{
taReceived.append("Sender=> " + senderText + "\n");

p.println(senderText);
p.flush();
p.close();
}//end else


}//end if

}//end second try

catch (IOException except)
{
taReceived.append(except.getMessage() + "\n");
}



}//end first try

catch (UnknownHostException hostExcept)
{
System.out.println("Unknown Host ") ;
hostExcept.printStackTrace();
}
}


Thanks for any help ye can give me
dex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top