java


What is the proper way to save a socket state and retrieve it later?


I am developing a multi user chat server in JAVA in which users can send private messages to each other and meanwhile they can chat in a chatroom, to do so I need to save the socket related to that user alongside the username somewhere for example in a table of active users in mySQL database so I can retrieve it later when user wants to chat with another available user in private. But the problem is the socket object is not serializable. And when I execute the following code and pass it a socket object :
public static void WriteObject(Object givemeObject){
try {
byte [] readbytes = null;
Connection Conn = DriverManager.getConnection(DB_URL,"root","somepassword");
PreparedStatement ps ;
String sql ;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(givemeObject);
oos.flush();
oos.close();
bos.close();
readbytes = bos.toByteArray();
sql = "INSERT INTO LOU VALUES 'abs0lute',(?)";
ps = Conn.prepareStatement(sql);
ps.setObject(1, readbytes);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
I get the exception of NOT Serializable .
Please take a look at code below :
while ((tempMessage=incomingBufferedReader.readLine()) != null) {
if (!isCommand(tempMessage)) {
for (Socket eachConn : listOfConnections) {
writer = getWriter(eachConn);
writer.println(usrName+": " +tempMessage);
writer.flush();
//writer.close();
}
} else {
DoAsCommanded(tempMessage);
}
}
This sends the received messages on stream to each available participant, what I want to do is as following prep code :
If (received message contains username indicator string )
parse the message to get the user name ;
find associated socket to that username ;
and println the received message to that particular socket( not the for loop ) so only that specific username can c the message ;
How can I retrieve the socket associated with that user name?
In other words, is there a way to mark sockets ?
Okay guys, I figured out the answer myself after a day of non-stop information collecting :
I created a class which had two instance variables :
One : Socket
Two : Username
And when any user connects to the server I create a new instance of that class and use the set methods to set those to instance variables .
Then I add the newly created object to a List of type Object , and when its necessary to retrieve it , I type cast them back to the Socket and String types.
Just wanted to share if you care .

Related Links

Autowiring based on method argument
read network file from Oracle database using JAVA
Why are redirects in servlets required to be absolute?
Spring Security 4 Not assigning admin role
Code keeps giving a InputMismatchException when I type 2 words
Itextpdf : set image in middle of text
How can I connect from client to a server with unknown ip address using java?
How to get context in JUnit4
Java: Showing a BoxLayout panel in the middle of the frame
Design a theme factory
Deploying a simple HttpServlet to TomEE from the command line
Can I convert a string to a java code [duplicate]
iText flatten full PDF while creating
Maven 2.5.1 doesn't support JDK 7 [duplicate]
java.lang.arrayindexOutOfBoundsEXCEPTION: 22 [duplicate]
Use Robolectrics with Mockito to mock/stub function call making http request, but it doesn't work

Categories

HOME
yii2
image
relayjs
bpmn
react-router
alfresco
read-eval-print-loop
rsyslog
gis
razor
google-oauth
rubygems
rdf
electronics
v8
icloud
windows-server
uber-api
directx
webrequest
primary-key
alignment
visual-studio-cordova
percona
evopdf
textfield
scichart
paging
tapestry
chromium-embedded
captiveportal
p-value
wpfdatagrid
msys2
bpel
http-status-code-503
preg-match-all
airconsole
mmenu
restlet
swift3.0.2
unobtrusive-validation
opshub
cookiecutter-django
hp-ux
pdf-reactor
botbuilder
nomethoderror
angular-resource
taffy
dds
hls.js
tropo
directory-structure
google-cdn
xcb
deadbolt-2
bstr
fuzzy-search
wapiti
hsv
metaclass
inmobi
jsapi
directoryservices
python-green
dd
id3v2
titanium-modules
eol
wp-query
valuechangelistener
transcoding
monomac
sitemesh
jmapviewer
hungarian-algorithm
delphi-6
cisco-jtapi
broken-links
cascalog
enter
hamachi
subviews
jquery-ui-droppable
sef
mtj
project-hosting
defensive-programming

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App