• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Implementing a simple java TCP server  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two readers plugged to the internal network. They just send data to the server machine, where I want to have this java app running receiving the data. It won't be that much data. Each reader could be transmitting single strings like "1234567" to a rate as much as maybe half a dozen times per second.

Without entering into much detail about the readers, they are datalogic and both are different models, but with this in common: They are configured to transmit the data via tcp/ip to a certain ip:port.

I tested the data trasmission with this software: https://www.hw-group.com//products/hercules/index_en.html to see if I'm receiving the data properly and it does check out, it works well.

The problem comes when I run my TCP server implementation: I receive the data from one of the devices 100% of the times, while the other one is hit and miss: Sometimes the data sent over the network never makes it to my app and I don't know why.

I'm pasting the code of what I'm using: It's simple but, with my java knowledge and after digging around the internet, it's the best I came up with.

Here's the java file with the main:




Here's TCPServer.java:



And here's TCPServerThread.java:






I tried to implement the server on a thread since I want to implement this into a bigger project in the future and I don't want that app locked in the loop that is waiting for connections.

Any clues on what should I do to see why I'm not receiving some of the data? Any suggestion on a better way to achieve this?

Thanks!
 
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

receive the data from one of the devices 100% of the times, while the other one is hit and miss:


Can you describe the differences between "one of the devices" and  "the other one"?
How are the clients connecting to this server?
 
Red Nano
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

receive the data from one of the devices 100% of the times, while the other one is hit and miss:


Can you describe the differences between "one of the devices" and  "the other one"?
How are the clients connecting to this server?



Well, they are Datalogic Matrix 200 and matrix 300n: They are the clients. I mean, using their configuration software, you tell them which IP and to which port to connect to, and they manage how they do it. There's not much more to configure on that side: IP, Net Mask and port.

I also noticed that the lower the connections per second, the less likely is this error to appear.

I'd say it seems like the reader sends the information and the TCP server just doesn't register it. What bugs me is that it's TCP protocol, which means (please, correct me if I'm wrong) that the client would retry the connection until the transfer is properly done.

It bugs me that it looks like an issue on the client's side but there's not much I can do. I was looking for maybe someone to tell me if my implementation was correct and if I could tweak something else to mitigate this issue: As I said, my knowledge is limited and implementing this is quite an achievement for me....


I also created a simple TCP client and I'm able to transmit 10k messages in some seconds without any loss at all.

I even tried to transfer data using netcat from an unix shell and it does work wonderful: 100k messages sent and 100k messages received.

I also am not sure if closing the connection after every data transfer is the way to go. If I don't close it, however, the server stops working, though...
 
Red Nano
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, they are Datalogic Matrix 200 and matrix 300n: They are the clients. I mean, using their configuration software, you tell them which IP and to which port to connect to, and they manage how they do it. There's not much more to configure on that side: IP, Net Mask and port.


BTW both readers transmit their info properly to the hercules software, however, only the matrix 200 model is the one giving me trouble: the other one, matrix 300n transmits 100% of the messages.... It's a weird thing...
 
Norm Radder
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I don't know anything about the matrix software you are using.  I normally use a browser or client code written in java for testing servers.
 
Red Nano
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know: It's really weird. Looks like between connections, the server is not listening and thus, any data being transmitted in that period gets lost. But it doesn't make any sens.
 
Norm Radder
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the interactions between a client and server?   What data is supposed to be sent each way? 
What is expected for each client?
 
Red Nano
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The client reads a codebar and decyphers it.

On a bad read (the product has passed and there has been no decypher) it sends an error character.
On a good read (the products codebar has been decyphered) it sends that codebar.

It's a "dumb" TCP transmission: There is a PC running the code on the OP, listening to a port for incoming connections.
The reader is configured with datalogic's proprietary software and configured telling the above's PC IP and port on which I'm listening for data.
When the reader transmits the data (either a good or a bad read) it just establishes a tcp connection, sends the raw data (no ssl, or login/authentication) and then disconnects. Each reader can do this up to 10 times per second on full speed.


On the server side I receive either a failure message which is usually something like "<stx>1<can>" <can> being decimal char "2" (indicating transmission start), that number "1" being a predefined character to identify which reader is sending the info, and <can> being decimal character 24 (indicating cancel, or a way to know there has been no successful read).

A good read would look like this:

<stx>10001<cr><lf>


A bad read would look like this:

<stx>1<can>


Thenm there's this other logic to control certain machinery: Long story short: Up until now I was doing this via COM ports but now those old readers broke, so we had to purchase new ones with TCP/IP: Set them on the same network as the controlling computer and rewrite the part that retrieves the data or is listening for data.
 
Norm Radder
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what a client does:
Connect to server's socket
send a message
disconnect from the server

The server:
wait for connect
when connected, read and display a message
disconnect from client

So each message uses a whole new connection.

What is printed out by the server that shows the problem?
 
Red Nano
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Is this what a client does:
Connect to server's socket
send a message
disconnect from the server

The server:
wait for connect
when connected, read and display a message
disconnect from client

So each message uses a whole new connection.

What is printed out by the server that shows the problem?



Well: This particular reader reads and transmits 50 messages and the TCP server only registers like half of them. If I slow down and read slower, maybe I can get a better change of reads (When I say reads I mean both good/and or bad.
If I open another software, like Hercules, to check if the reader is actually sending the data, I get 100% received data, which means for some reason, for this particular reader, my TCP server is not receiving the data.
Weird, I know...
 
Norm Radder
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This particular reader reads and transmits 50 messages 


I assume by reader you mean the client in the context of the server with clients sending it messages.
Does the client open a new socket for each message before sending the message?
The server closes the socket immediately after reading from it.


the TCP server only registers like half of them.


What is printed out   by the server  that shows what you are talking about?
 
Red Nano
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

This particular reader reads and transmits 50 messages 


I assume by reader you mean the client in the context of the server with clients sending it messages.
Does the client open a new socket for each message before sending the message?
The server closes the socket immediately after reading from it.


the TCP server only registers like half of them.


What is printed out   by the server  that shows what you are talking about?



Yes, by reader, I mean client.
I was checking wireshark to see if there is any connection on the client's startup (when i power the reader on, before transmitting any data) and there is some activity, which means it does establish a connection. Can the problem be that I close the connection after receiving data and the server has to re-open it again and that's why I lose the data?

I know the server only receives half of the data because i set a counter on the server and cause the client to send 50 messages, and the server only counts that between 20 and 30 are received. If i read every, say, two seconds, I perhaps can receive all the data and not lose any. Again, pointing to the possibility that I'm closing the connection and screwing it up.
 
Norm Radder
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the client do about connections?  Does it connect before sending every message? 
What does the client do when the server disconnects after the server receives a message?

What is printed out   by the server  that shows what you are talking about?
 
Red Nano
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:What does the client do about connections?  Does it connect before sending every message? 
What does the client do when the server disconnects after the server receives a message?

What is printed out   by the server  that shows what you are talking about?



Before sending any data, I see the client connecting, so I assume it establishes a connection before sending data.
After the server disconnects, the client seems to connect again, even if there's still no message to send.
Thus I can assume the client always tries to keep an open connection, while my server closes every time a message is received.

I see you asked the third question again :P . I didn't try to be rude, I just didn't know what you meant exactly: I capture the message sent by the client and print it on the System.out.println:



That's all I see, a bunch of lies like:

New client from: 192.168.1.56
Message Received: 1002
Socket Closed
New client from: 192.168.1.56
Message Received: 1003
Socket Closed
New client from: 192.168.1.56
Message Received: 1004 <- Message 1005 skipped, but confirmed read by the client on the hardware itself (green light and confirmation beep)
Socket Closed

Thanks for insisting I'm having a really hard time figuring out how to proceed.
 
Red Nano
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction on the last lines (for some reason, I can't edit my posts, sorry about the mess)

That's all I see, a bunch of lies like:

New client from: 192.168.1.56
Message Received: 1002
Socket Closed
New client from: 192.168.1.56
Message Received: 1003
Socket Closed
New client from: 192.168.1.56
Message Received: 1005 <- Message 1004 skipped, but confirmed read by the client on the hardware itself (green light and confirmation beep)
Socket Closed
 
Red Nano
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I've made some advance:




I'd have to do more intensive testing, but it looks like it doesn't lose messages right now.... will report tomorrow since I'm exhausted for the day.

Thanks!!!
 
author
Sheriff
Posts: 23603
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Red Nano wrote:
Before sending any data, I see the client connecting, so I assume it establishes a connection before sending data.
After the server disconnects, the client seems to connect again, even if there's still no message to send.
Thus I can assume the client always tries to keep an open connection, while my server closes every time a message is received.



Yeah, don't do that. If the sender wants an always up connection, then don't disconnect. Constantly disconnecting (and forcing a re-connection) is very expensive !!

Henry
 
Henry Wong
author
Sheriff
Posts: 23603
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Red Nano wrote:
New client from: 192.168.1.56
Message Received: 1002
Socket Closed
New client from: 192.168.1.56
Message Received: 1003
Socket Closed
New client from: 192.168.1.56
Message Received: 1005 <- Message 1004 skipped, but confirmed read by the client on the hardware itself (green light and confirmation beep)
Socket Closed



If I have to speculate, you have a race condition. The client sends message 1003. While your server is processing message 1003, the client sends message 1004. When your server tears down the socket, it will take message 1004 with it. The client then re-connects and sends message 1005.

Henry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, they are Datalogic Matrix 200 and matrix 300n: They are the clients. I mean, using their configuration software, you tell them which IP and to which port to connect to, and they manage how they do it. There's not much more to configure on that side: IP, Net Mask and port.


How did you configure this? I am new to Datalogic Matrix and I'm trying to use a Datalogic Matrix 300n as a tcp client, but I did not find any docs teaching how to do that. Thanks!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!