• Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket connections on LAN?  RSS feed

 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am designing a java application which will have 100 clients and a database server over LAN. There is no application server, only core java is used to write the programming logic. Now, to read, update or write anything to the database a bare socket connection will be made by the client to the server over LAN. Please tell me is it safe to perform this kind of communication? Will the network connection sustain for a long time or there are many chances that it may break easily? In no case will I be able to use an application server. Can I design such an application that communicates through bare sockets with no protection as such?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A socket comunication in a LAN should be working for a long time, unless there was a problem with the LAN or the two programs participating in the comunication.

For accessing a database you will need JDBC. And there is a forum for the matter in the Ranch of course
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can defiantly use sockets for this type of application. The connection will stay alive as long as network is ok. Also, you can add logic to reconnect if the connection is lost (IOException is thrown for example). If your client does not send data all the time, use keepAlive() method to keep the connection opened. Also to speed up the process, try to decouple reader threads and DB writers.
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know about JDBC , actually i am to abstain from using a RDBMS.i fact i am desinging my own mini-dbms which provides for concurrency,transactions and relationships.hence my database connectivity will be through a bare socket from the client to the databse server application.

thanks for the info on sockets. , as i proceed with my project, i will need to inquire about the types of socket exceptions that can be thrown. i will come back to ask about that.Thanks again.
 
kriti sharma
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaroslav :

please explain in more detail and give some more directions:

1)Also, you can add logic to reconnect if the connection is lost (IOException is thrown for example).

2)If your client does not send data all the time, use keepAlive() method to keep the connection opened

3)Also to speed up the process, try to decouple reader threads and DB writers.

Thanks.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. here is a typical way of sending data

if (socket != null && socket.isConnected()) {
try {

log.debug("Sending dat");
socket.getOutputStream().write( message);
socket.getOutputStream().flush();

}
catch (IOException e) {

try {
socket.close();
socket = null;
}catch(IOException ioe){
}
}

U can add logic to reconnect/resend data after IOException is thrown.

2. Use setKeepAlive(true) after the socket is connected. This will keep connection open even if the line is idle.

3. U can have threads reading data from sockets and putting it on a queue/buffer then you have another set of tthreads to read the buffer and process data. This ASYNC design will speed up the process of reading/processing data by eliminating IO blocks.

I Hope that helps.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want also to have a look at Custom networking in the Java Tutorial.


2. Use setKeepAlive(true) after the socket is connected. This will keep connection open even if the line is idle.

I have never used that option but from Unix Network Programming by W. Richard Stevens:

When the keepalive option is set for a tcp socket and no data is exchanged accross the socket in either direction for two hours, TCP automatically sends a tcp probe to the peer:
A) the peers responds with the expected ACK. The application is not notified because all is ok.
B) The peer responds with a RST, which tells the local TCP that the peer host has crashed and rebooted. The socket is closed.
C) There was no response from the peer to the keepalive probe ... The probe is repeated eight times with a lapse of 75 seconds between them.

............

This option is normally used by servers. Because they spend more of their time blocked waiting for input across the TCP connection. But it the client peer crashes the server will never know about it, and the server will continue waiting for input that can never arrive. The keepalive option will detect this and will terminate the connection.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,


I use keepAlive() only for keeping idle connections alive (my FWs close inactive connections).
You would know that the connection crashed because on the server you are probably in the IO block ( for example line = reader.readLine() - reading line of data from socket) and that will throw IOException.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!