new socket created for each message sent.! help!

i have made a client application and a server application. the server application is made to listen for requests on port 9090 on loop back ip = 127.0.0.1. the client is like a chat programm sending messages. the server receives this message and is supposed to sent it back to the client and only then is the client supposed to dispaly it in the message history. the program runs fine with multiple clients also but there is a weird problem.

instead of having one socket object per connection it is creating a socket object each time 'send' is clicked in the client. and this is clearly not what i want as it would create problems in future when i make two clients talk to each other. here is the server code split accross two classes. class Server and class SocketHandler.

//a server that can receive and send data to client.

package server;

import java.io.*;

import java.net.*;

class Server

{

publicstaticvoid main(String[] args)

{

try

{

ServerSocket ss =new ServerSocket(9090);//Opens a new ServerSocket.

while(true)//Listens endlessly for a socket connection

{

Socket sc = ss.accept();// returns a socket object when connection is requested by the client.

//Creates a new SocketHandler object, passes the referrence of the socket object as an argument to its constructor and attaches a new thread to work on it.

new Thread(new SocketHandler(sc)).start();

}

}

catch(IOException e)

{

System.out.println("IOException. Cannot initialize server");

}

}

}

package server;

import java.io.*;

import java.net.*;

class SocketHandlerimplements Runnable

{

Socket soc;// handle to refer a socket object.

// variable to count number of socket objects created depenging on number of time constructor invoked

staticint i =0;

SocketHandler(Socket sc)//constructor to accept handle to a socket object.

{

soc = sc;//assign the handle of socket object to instance variable soc.

i++;//increments socket count.

System.out.println(i);//display the number of the socket connection.

}

publicvoid run()

{

try

{

//define in object to accept string.

BufferedReader in =new BufferedReader(new InputStreamReader(soc.getInputStream()));

//define out object to send a string.

PrintWriter out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(soc.getOutputStream())),true);

while(true)//runs as long as client doesnot close the connection.

{

String str = in.readLine();//receive message from client.

out.println(str);//send it back to client.

}

}

catch(IOException e)

{

System.out.println("IO Exception");

}

}

}

[5072 byte] By [kothari_neerava] at [2007-11-27 7:48:47]
# 1
Do you mind posting the client's code for sending the message?
quittea at 2007-7-12 19:29:40 > top of Java-index,Java Essentials,Java Programming...
# 2

here is the client code.

(sorry if there have been many similar threads as my browser was not responding properly so clicked 'post' a couple of times.)

the client code is huge as it has the gui. so i'll post that part which does the message sending.

ClientGUI.java

private void sendActionPerformed(java.awt.event.ActionEvent evt) {

String toSend = message.getText(); //extract the string to send from message textarea

String received;//declate new string variable for string received from server

try

{

received = Client.sendAndReceive(toSend);//send the string and receive it back from server

if(i==0)//if this is the first message then...

{

history.setText(received); //... set text to the first message

i=1;

}

else//for successive messages

{

history.append("\n");//go to new line and...

history.append(received);//... append message.

}

}

catch (IOException e)

{

jDialog1.setVisible(true);//incase of exception display exception dialog.

}

message.setText("");//clear the current message to type a new one.

}

package client;

import java.io.*;

import java.net.*;

class Client

{

public static void main(String[] args)

{

ClientGUI gui = new ClientGUI();

gui.setVisible(true);

}

static String sendAndReceive(String str) throws IOException

{

InetAddress ip = InetAddress.getByName("127.0.0.1");//Defines the ip address to ping to

Socket soc = new Socket(ip,9090);//Opens a new Socket object

//Makes a new out object.

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(soc.getOutputStream())),true);

//define in object to accept string.

BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));

out.println(str);

return(in.readLine());

}

}

kothari_neerava at 2007-7-12 19:29:40 > top of Java-index,Java Essentials,Java Programming...
# 3
Well, there you go:> Socket soc = new Socket(ip,9090);You create a new socket in each call to sendAndReceive(), which is called by the button's action handler.
quittea at 2007-7-12 19:29:40 > top of Java-index,Java Essentials,Java Programming...
# 4
oh heck! dumb me. thanks a lot. how could i be so ignorant!
kothari_neerava at 2007-7-12 19:29:40 > top of Java-index,Java Essentials,Java Programming...
# 5

> oh heck! dumb me. thanks a lot. how could i be so

> ignorant!

Don't let it worry you... I once spent three hours trying to find the two zero's in

#define Z00M_LEVEL 0

It gets everyone some day.

Message was edited by: corlettk

PS: thanx for the code.

corlettka at 2007-7-12 19:29:40 > top of Java-index,Java Essentials,Java Programming...