TCP Socket Problem

01 Jan 2011

Hello,

I tried to send simple Data over TCP. I tested the UDP Demo Programm and it works. http://mbed.org/users/donatien/programs/UDPSocketExample/5yuvy

So there is no Hardwareproblem.

Now I wrote an Code for TCP. But I cannot receive any Data. Can you help?

#include "mbed.h"

#include "EthernetNetIf.h"

#include "TCPSocket.h"

 

EthernetNetIf eth(

IpAddr(192,168,1,2), //IP Address

IpAddr(255,255,255,0), //Network Mask

IpAddr(192,168,1,1), //Gateway

IpAddr(192,168,1,1)  //DNS

);

 

TCPSocket tcp;

 

int main() {

printf("Setting up...\n");

EthernetErr ethErr = eth.setup();

if(ethErr)

{

printf("Error %d in setup.\n", ethErr);

return -1;

}

printf("Setup OK\n");

 

Host server(IpAddr(192, 168, 1, 1), 12345);

 

tcp.bind(server);

 

Timer tmr;

tmr.start();

while(true)

{

Net::poll();

if(tmr.read() > 5)

{

tmr.reset();

const char* str = "Hello world!";

tcp.send(str, strlen(str));

printf("%s\n", str);

}

}

}

 

02 Jan 2011

I am an idiot!!! Sry for asking this silly question.

I forgot to connect to the socket...

22 Jan 2011

Hi, i have tried the same program with connect instead of bind, but i still cannot connect to socket. It gives me error 65531 when trying to connect

22 Jan 2011

try my test programm. it should work...

do you have a programm on the computer which listens to the right port???

22 Jan 2011

Hey Dominik, i tried the test program. It still does not work. I can see in the connection status that some data is sent by mbed, but it gives the connect error of -65531

22 Jan 2011

do you receive data on your computer? do you have a network analyser tool like wireshark???

you must have a programm on your computer, that answers to mbed...

22 Jan 2011

I have tera term client. It cannot connect to mbed. It says connection refused, but some few bytes of data is transferred in the connection status

22 Jan 2011

the programm i wrote is a client. it can connect to an server. the server must be running on the computer.

as far as i know tera term is just an client. you cannot connect to mbed with it. you have to write an socket server programm on your computer. google for socket in your favourite programming language...

22 Jan 2011

oh, i did not realize it. Thanks, let me try

14 Mar 2011

Thanks Dominik, that's the first program I'm getting results with that I like :)

I'm running your program (with DHCP, EthernetNetIf Eth; ) Filled in my PC's IP adress on line 64 of your program: Host server(IpAddr(#,#,#,#), 12345);

Running http://www.pythonprasanna.com/Papers%20and%20Articles/Sockets/tcpserver.c on my PC. Filled in socket 12345 on line 32: server_addr.sin_port = htons(12345);

The tcpserver.c sees the incoming connections. When I type something (not q or Q) in my editor (eclipse in my case, on ubuntu) I receive:

TCPServer Waiting for client on port 12345
I got a connection from (###.###.###.### , #####)
SEND (q or Q to quit) : receive

RECIEVED DATA = Testdata to send: 1234567890abc...Testdata to send: 1234567890abc...Testdata to send: 1234567890abc... 

See http://www.binarii.com/files/papers/c_sockets.txt for some more basic info about the tcpserver.c

Time to edit stuff >.>

Thanks again!

Melchior

15 Mar 2011

I've been trying to make the mbed the server. I've set Host local(IpAddr("<mbed's IP>")12345) and TCPSocket tcp; I've bound the socket to the port with tcp.bind(local);

So far all goes well.

Then I do tcp.listen(); and it "crashes". I know the next step is to get a connection request from a client on the bound port, but is it supposed to hang on tcp.listen(); like I'm experiencing?

The code thus far:

Import programTCPTest

TCPTest Server attempt

The results:

Welcome to wireFUSE
Setting up...
[..\fwk\if\eth\EthernetNetIf.cpp:setup@86] HW Addr is : 00:02:f7:f0:3a:26.
[..\fwk\if\eth\EthernetNetIf.cpp:setup@99] DHCP Started, waiting for IP...
[..\fwk\if\eth\EthernetNetIf.cpp:setup@142] Connected, IP : 130.144.3.9
Setup OK
Init bind..
Bound to port
Init listen..

Am I doing something wrong? Or do I just need to send a connection request?

Edit: Running wireshark I can confirm that my PC is sending connection requests to the mbed.

No.. | Time..... | Source...... | Destination | Protocol | Information
2623 | 71.218583 | 130.144.2.83 | 130.144.3.9 | TCP..... | 34184 > italk [SYN] Seq=0 Win=5840 Len=0 MSS=1460 TSV=4335656 TSER=0 WS=6

Edit2: http://mbed.org/users/xshige/programs/TCP_server/leamu9/docs/main_8cpp_source.html helped :)

Import programTCPTest

TCPTest Server attempt

17 May 2011

My mbed is sending some characters to the PC over TCP.I don't want to write Host(PC) IP address in the program. Can I get Host(PC) IP address during the execution of the program?

17 May 2011

sonali lagu wrote:

Can I get Host(PC) IP address during the execution of the program?

You can do a DNS query.

17 May 2011

Hendrik Lipka wrote:

sonali lagu wrote:

Can I get Host(PC) IP address during the execution of the program?

You can do a DNS query.

Thanks Hendrik... I have one more question. I try to connect socket to the host repeatedly with interval of 5 sec. it gets connected for the first time but after that i get 'bind error' and still I am able to send and receive data using TCP. shall I ignore the error?

I am working on TCP and mbed for the 1st time... sorry for asking such silly question!

17 May 2011

sonali lagu wrote:

I try to connect socket to the host repeatedly with interval of 5 sec. it gets connected for the first time but after that i get 'bind error' and still I am able to send and receive data using TCP. shall I ignore the error?

I would need to see the code for that. If you get a bind error I think this means that you may have the old connection still open. Are you sure you are closing it properly? I would recommend, though, to leave it open if you need it continuously.