SBC Socket problem

Supporting Java SE version 7 and up
_Dejan_

SBC Socket problem

Postby _Dejan_ » Sat Feb 26, 2011 10:39 am

I have problem with Socket comunication on SBC with TCP server. Im try many examples from net but none of them do not work.
For example on SBC I put on AttachListener event next:

Code: Select all

MainPanelIO.addAttachListener(new AttachListener() {
            public void attached(AttachEvent ae) {
               System.out.println("attachment of " + ae);
               Socket clientSocket = new Socket();
                DataInputStream is = null;
                PrintStream os = null;
                DataInputStream inputLine = null;
               
                try   {
                  SocketAddress address = new InetSocketAddress("192.168.1.10", 1234);
                  try {
                     clientSocket.connect(address, 5000);
                  }catch (SocketException ex){}
                  
                  os = new PrintStream(clientSocket.getOutputStream());
                     is = new DataInputStream(clientSocket.getInputStream());
               }catch (Exception ex) {System.out.println("ServerConnection.Connect() Error: " + ex.getMessage());
               }
               
               String data = "ABCDEF";
               String responseLine = "";
               if (clientSocket != null && os != null && is != null) {
                     try {
                        inputLine = new DataInputStream(new ByteArrayInputStream(data.getBytes()));
                         os.println(inputLine.readLine());
                         while ((responseLine = is.readLine()) != null) {
                             if (responseLine.indexOf(data.substring(0,6)) != -1) {
                                break;
                             }
                         }
                     } catch (IOException e) {
                         System.err.println("IOException:  " + e);
                     }
               }
               System.out.println(responseLine);
            }
         });




I get in log:

Code: Select all

attachment of PhidgetInterfaceKit v826 #46331 (attached)
java.net.SocketException: Numerical result out of range
   at gnu.java.nio.VMChannel.getpeername(Native Method)
   at gnu.java.nio.VMChannel.getPeerAddress(VMChannel.java:470)
   at gnu.java.nio.SocketChannelImpl.isConnected(SocketChannelImpl.java:194)
   at gnu.java.nio.SocketChannelImpl.connect(SocketChannelImpl.java:148)
   at gnu.java.net.PlainSocketImpl.connect(PlainSocketImpl.java:281)
   at java.net.Socket.connect(Socket.java:454)
   at MainPanel$1.attached(SBCDemo.java:211)
   at com.phidgets.Phidget.fireAttach(Phidget.java:638)


SBC connect to server but server do not receive any data until I kill application on SBC(killall -9 AppName). If I run this code on PC in java run ok send ABCDEF to server and server reply with OK=ABCDEF

Any sugestion what is wrong with SBC fw and how I can normaly use TCP client on it?

RBBT
Phidgeteer!
Posts: 90
Joined: Sun Nov 15, 2009 2:16 pm
Location: Lower Bavaria (Germany)
Contact:

Re: SBC Socket problem

Postby RBBT » Mon Feb 28, 2011 12:28 am

Hello,

I don't understand your code (sorry), but for test purposes you can compile and run the KnockKnockServer example from here:

http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html


This is a good entry point for a simple client / server app.
Best Regards from Lower Bavaria
RBBT
http://www.robobuam.de/

_Dejan_

Re: SBC Socket problem

Postby _Dejan_ » Mon Feb 28, 2011 12:57 am

RBBT wrote:Hello,

I don't understand your code (sorry), but for test purposes you can compile and run the KnockKnockServer example from here:

http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html


This is a good entry point for a simple client / server app.


Hi,
Im try this example but doesn't work.
client only connect to socket and do not send anything, until I kill application on SBC.
what I must to do is send some string to server(TCP Server written in Delphi) which send some string back ...
all other devices(GPS Tracking modules) work normaly with this Delphi TCP Server(Send string and receive it back from server) also on PC java application work ok but in SBC do not work.

RBBT
Phidgeteer!
Posts: 90
Joined: Sun Nov 15, 2009 2:16 pm
Location: Lower Bavaria (Germany)
Contact:

Re: SBC Socket problem

Postby RBBT » Mon Feb 28, 2011 3:36 am

Im try this example but doesn't work.

Do you got exceptions or compile time errors?
Best Regards from Lower Bavaria
RBBT
http://www.robobuam.de/

_Dejan_

Re: SBC Socket problem

Postby _Dejan_ » Mon Feb 28, 2011 4:12 am

Sorry but I don't understand what you want...
When Im compile it I didn't receive any error some warnings but without error. If I run same code from eclipse it connect to board by USB, connect to server, sucessfuly send string and receive it. but when I run jar file on SBC it stuck after connect to TCP server and give error which Im write in previous post...

RBBT
Phidgeteer!
Posts: 90
Joined: Sun Nov 15, 2009 2:16 pm
Location: Lower Bavaria (Germany)
Contact:

Re: SBC Socket problem

Postby RBBT » Mon Feb 28, 2011 7:09 am

Invoke InetSocketAddress.isUnresolved() - if it returns true, there is a problem with the address (firewall etc...).

Another question: Why do you not catch the SocketException behind

Code: Select all

clientSocket.connect(address, 5000);
???
Best Regards from Lower Bavaria
RBBT
http://www.robobuam.de/

_Dejan_

Re: SBC Socket problem

Postby _Dejan_ » Mon Feb 28, 2011 12:08 pm

RBBT Thanks for your help and time.
Im decide that before I write this answer make one demo for you and other who would like help find me solution for my problem TCP Socket problem...
In attachment(Test.rar) you will find TCP client which Im try on PC and SBC. Im upload whole Eclipse project that you can easy import it...
You will in source code see Hostname and Port and change it to your IP and port. It send "ABCDEF" and need response from server: "ABCDEF" or "ABCDEF1"... Response from server must include received string.

If I run it on PC I get next output:

Code: Select all

atachment of PhidgetInterfaceKit v201 #121095 (attached)
Received string from server: ABCDEF99
PhidgetInterfaceKit v201 #121095 (attached) input 0 changed to false
PhidgetInterfaceKit v201 #121095 (attached) input 1 changed to false
PhidgetInterfaceKit v201 #121095 (attached) input 2 changed to false
PhidgetInterfaceKit v201 #121095 (attached) input 3 changed to false
PhidgetInterfaceKit v201 #121095 (attached) input 4 changed to false
PhidgetInterfaceKit v201 #121095 (attached) input 5 changed to false
PhidgetInterfaceKit v201 #121095 (attached) input 6 changed to false
PhidgetInterfaceKit v201 #121095 (attached) input 7 changed to false
PhidgetInterfaceKit v201 #121095 (attached) output 0 changed to false
PhidgetInterfaceKit v201 #121095 (attached) output 1 changed to false
PhidgetInterfaceKit v201 #121095 (attached) output 2 changed to false
PhidgetInterfaceKit v201 #121095 (attached) output 3 changed to false
PhidgetInterfaceKit v201 #121095 (attached) output 4 changed to false
PhidgetInterfaceKit v201 #121095 (attached) output 5 changed to false
PhidgetInterfaceKit v201 #121095 (attached) output 6 changed to false
PhidgetInterfaceKit v201 #121095 (attached) output 7 changed to false
PhidgetInterfaceKit v201 #121095 (attached) sensor 0 changed to 0
PhidgetInterfaceKit v201 #121095 (attached) sensor 1 changed to 0
PhidgetInterfaceKit v201 #121095 (attached) sensor 2 changed to 0
PhidgetInterfaceKit v201 #121095 (attached) sensor 3 changed to 0
PhidgetInterfaceKit v201 #121095 (attached) sensor 4 changed to 0
PhidgetInterfaceKit v201 #121095 (attached) sensor 5 changed to 0
PhidgetInterfaceKit v201 #121095 (attached) sensor 6 changed to 0
PhidgetInterfaceKit v201 #121095 (attached) sensor 7 changed to 0


If I run it on SBC I get this output:

Code: Select all

atachment of PhidgetInterfaceKit v826 #46331 (attached)
java.net.SocketException: Numerical result out of range
   at gnu.java.nio.VMChannel.getpeername(Native Method)
   at gnu.java.nio.VMChannel.getPeerAddress(VMChannel.java:470)
   at gnu.java.nio.SocketChannelImpl.isConnected(SocketChannelImpl.java:194)
   at gnu.java.nio.SocketChannelImpl.connect(SocketChannelImpl.java:148)
   at gnu.java.net.PlainSocketImpl.connect(PlainSocketImpl.java:281)
   at java.net.Socket.connect(Socket.java:454)
   at java.net.Socket.connect(Socket.java:414)
   at java.net.Socket.<init>(Socket.java:315)
   at java.net.Socket.<init>(Socket.java:164)
   at ServerConnection.<init>(Test.java:33)
   at Test$1.attached(Test.java:88)
   at com.phidgets.Phidget.fireAttach(Phidget.java:638)


Application status on SBC is:
Application is running...
And must be killed by SSH:
killall -9 jamvm

Im make one quick demo server which I use for testing which is on this link: LINK

Thanks again.
Attachments
Test.rar
TCP Client
(13.65 KiB) Downloaded 153 times

RBBT
Phidgeteer!
Posts: 90
Joined: Sun Nov 15, 2009 2:16 pm
Location: Lower Bavaria (Germany)
Contact:

Re: SBC Socket problem

Postby RBBT » Tue Mar 01, 2011 11:13 am

Hi,
sorry, but I can not help. I had no chance to get your code running.
I don't use the phidgets21.jar on my development machine, so it's not possible to compile your code.

And again: this is not the way to do client / server programming in Java. My suggestion is to compile the KnockKnock example.

you say "...And must be killed by SSH:" that's ok, because
you use a while loop. You have to start an external thread.

Another mistake ist to transform a string to bytes and send it over the network. This is not necessary, because in Java you can send primitives and strings in one stream. The only thing you have to do is to use the appropriate I/O stream.

Good luck.
Best Regards from Lower Bavaria
RBBT
http://www.robobuam.de/

_Dejan_

Re: SBC Socket problem

Postby _Dejan_ » Tue Mar 01, 2011 1:56 pm

I realy don't understand why on PC my code run great and why on SBC doesn't work...
Server can't and will not be change to java server. Server work great with all other devices, putty(telnet), native telnet in windows only with SBC do not work ...
So lets try fix Client and try explain what I need...
I will have main class which will first check all avalible phidgets(minimum will be 1x SBC, 1xTextLCD, 1xRFID max for now 1xSBC, 4xTextLCD, 4xRFID).
After checked all phidgets I will estabilish TCP connection to server, send some data(strings) and receive reply's from server. After this initialization TCP connection must be estabilished all time because server can send to client some data and SBC must send him reply.
After this I will create module class(1 module have 1xTextLCD and 1xRFID) one SBC can have up to 4 modules. All modules must comunicate with server trough estabilished connection in main class.
If connection drop must be estabilished again, send some data, check if logged client(by RFID) is stil active and a lot of other functions...


Return to “Java”

Who is online

Users browsing this forum: No registered users and 2 guests