Storing Java Sockets in a mysql database

I have a multithreaded application and I am trying to store the socket variable in a database the database column is a text and it is a mysql database here is my retrieval method:

public Socket getUser1(String game){

Object id =null;

try{

Statement stmt = connection.createStatement();

// Prepare a statement to insert a record

String sql ="SELECT * FROM currentGames where gameid=\"" + game +"\"";

ResultSet rs = stmt.executeQuery(sql);

while (rs.next()){

id = rs.getObject(4);

}

}catch (SQLException e){

e.printStackTrace();

}

//System.out.println("Returning id: " + id);

Socket s =null;

s = (Socket)id;

return s;

}

my insert method is as follows:

public String createNewGame(Object socket){

String id ="";

try{

Statement stmt = connection.createStatement();

// Prepare a statement to insert a record

String sql ="INSERT INTO currentGames (user1Socket,currentTurn) values ('" + socket +"','x')";

// Execute the insert statement

stmt.executeUpdate(sql);

sql ="SELECT * FROM currentGames where user1Socket=\"" + socket +"\"";

//System.out.println(sql);

ResultSet rs = stmt.executeQuery(sql);

while (rs.next()){

id = rs.getString(1);

}

sql ="INSERT INTO currentBoards (game) values ('" + id +"')";

// Execute the insert statement

stmt.executeUpdate(sql);

}catch (SQLException e){

e.printStackTrace();

}

//System.out.println("Returning id: " + id);

return id;

}

Everything inserts fine here is an example of the data in the mysql database:

Socket[addr=/192.168.17.109,port=51064,localport=5900]

however when I try to turn that connection back to a socket to create a temporary printwriter to send data to it I always get:

java.lang.ClassCastException: java.lang.String

at game.sqlConnection.getUser1(sqlConnection.java:104)

at game.startServerThread.run(startServer.java:218)

This is how I use the getUser1 example

Socket user1 = sql.getUser1(game);

PrintWriter user1pw =new PrintWriter(user1.getOutputStream(),true);

System.out.println(line);

user1pw.println(line);

I know if I save the item in a vector it works fine but I would really like to save all the sockets in a database rather then a vector because some of my other methods.

I have tried the mysql column types:

blob

text

varchar

All produce the same result that it can't cast from a string. Please help this is driving me CRAZY.

--John

[4013 byte] By [john8675309a] at [2007-10-3 2:48:53]
# 1
A Socket is a transient endpoint of a connection between two processes, and it has no meaning outside the process that created it. You can't store a Socket anywhere. Such an operation makes no sense whatsoever.What exactly are you trying to accomplish?
ejpa at 2007-7-14 20:37:39 > top of Java-index,Archived Forums,Socket Programming...
# 2

By storing the socket I can open a printwriter to that socket at anytime. I have stored sockets in vectors before and it worked.

I am making a simple tic tac toe game with a client and a server

the server can handle multiple games at the same time. That is where the db comes into play once a user connects to a game number say game 500. The server can query the database by game id and pull the stored sockets from the database to send messages to each of the clients in the game.

sockets.addElement(client);

Socket cl = (Socket) sockets.elementAt(i);

//send to all sockets:

for (int i=0;i<sockets.size();i++) {

Socket cl = (Socket) sockets.elementAt(i);

PrintWriter o = new PrintWriter(cl.getOutputStream(),true);

o.println("e12x0000" + " " + this.user);

}

(the above code is just a snippet from a working project but just snippets)>

john8675309a at 2007-7-14 20:37:40 > top of Java-index,Archived Forums,Socket Programming...
# 3

> By storing the socket I can open a printwriter to

> that socket at anytime.

You can't store a socket in a database, or anywhere outside the process.

> I have stored sockets in

> vectors before and it worked.

Within the same process.

> The

> server can query the database by game id and pull the

> stored sockets from the database to send messages to

> each of the clients in the game.

Nope, that will never work. Forget it, find a real solution.

> (the above code is just a snippet from a working

> project but just snippets)

Yes I understand how to write a Vector of Sockets but you can't put them into a database.

ejpa at 2007-7-14 20:37:40 > top of Java-index,Archived Forums,Socket Programming...
# 4
What would a real solution be, I used Vectors temporary use. Any ideas on how the solution can be implemented?Thanks,-John
john8675309a at 2007-7-14 20:37:40 > top of Java-index,Archived Forums,Socket Programming...
# 5
Map<Game,Socket>?
ejpa at 2007-7-14 20:37:40 > top of Java-index,Archived Forums,Socket Programming...
# 6

Find what attributes needs a Socket instance to be made 'alive', and store these attributes into your DB, with key reference to the game ID.

When your application needs to establish a socket connection for a defined game ID, just load the Socket's attributes to bring it 'alive' again.

Of course, as soon as a socket has been connected, you won't have to query the DB until the socket gets closed. This implies keeping a map of all active sockets.

Franck_Lefevrea at 2007-7-14 20:37:40 > top of Java-index,Archived Forums,Socket Programming...
# 7

> Find what attributes needs a Socket instance to be

> made 'alive', and store these attributes into your

> DB, with key reference to the game ID.

> When your application needs to establish a socket

> connection for a defined game ID, just load the

> Socket's attributes to bring it 'alive' again.

>

> Of course, as soon as a socket has been connected,

> you won't have to query the DB until the socket gets

> closed. This implies keeping a map of all active

> sockets.

The OP suggests that the server is the source of the data.

In normal circumstances a server is not going to be able to initiate a connection to a client.

jschella at 2007-7-14 20:37:40 > top of Java-index,Archived Forums,Socket Programming...