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

Odd NullPointerException when using File[] in Java [duplicate]
how do I call a spring mvc REST controller from angular.js?
Reads CSV File and IF statement value 0?
Spring Integration enrichHeader with randomUUID
Configure maven lifecycle mapping for native plugin
How to check 2 conditions in a try block before executing catch block - Selenium WebDriver Java
Failed to bounce to type with push Id Firebase
libgdx Screen - How to set a new Screen by a String
How to send UNIX ftp commands using JSch API?
How to create a stub for BusinessException which extends Exception class
Converting output back to hours minutes seconds, after converting to seconds for comparison
Jackson Polymorfic #JsonTypeInfo type attribute always null
Getting a grey level image back from a color image in android after bitwise extraction
Generic Issue in LocalDate
Moving point on canvas in time, speed in Android (java)
Excel 2007 file corrupted after being created in Apache POI

Categories

HOME
netsuite
cookies
gremlin
mfc
hp-exstream
fme
analysis
cmd
acquia
node-notifier
cross-validation
slick-slider
ssl-client-authentication
kentor-authservices
fortumo
percona
iolanguage
facebook-page
reactive-cocoa
text-rendering
nodatime
nas
java-3d
jasonette
css-animations
tapestry
angular2-aot
django-storage
status
react-css-modules
elasticsearch-ruby
compatibility
jspm
c++-amp
stormpath
data-manipulation
subset-sum
rotational-matrices
nssegmentedcontrol
dosbox
leading-zero
temporary-files
user-controls
dism
broadcastreceiver
unoconv
grid.mvc
dynamic-reports
elmah
bootstrap-dialog
http-redirect
checkboxlist
google-datalayer
komodoedit
revapi
pnotify
blogengine.net
idisposable
lowpass-filter
colorama
forever
csound
pg-dump
sybase-asa
windows-mobile-6.5
dynamics-sl
redundancy
eventkit
migradoc
nested-sets
log4c
elements
azure-virtual-network
skobbler-maps
clang-static-analyzer
moveit
captivenetwork
asp.net5
pervasive-sql
execute
endeca-workbench
sortedlist
skos
csquery
tween
eol
uitouch
expected-exception
reactfx
geos
quantlib-swig
random-seed
flashvars
specification-pattern
anonymous-methods
custom-backend
php-parser
libc++
horizontal-accordion
ecl
android-sdk-2.1
radcombobox
ncqrs
memory-size
eqatec
pascal-fc
mediarss
rtml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App