I am using MCB1700 as TCP server and I can received data from PC client. But I can not send data back to the PC client while the TCP is still connected. Is there any one who has experience to do that ?
In the /TCPnet/LEDSwitch example, I managed to receive data from PC client but I don't know how to send the response/data back to the PC client? which function can be used ? Is there any one who has example ?
use tcp_send function, dont use it inside the callback and make sure that you are using the tcp_get_buf function before sending data:
ucSendbuf = tcp_get_buf (10); .... .. populate ucSendbuf byte by byte .. .... tcp_send (tcp_soc, ucSendbuf, 10);
Hi Christian
Thank for the reply. And sorry for the late response due to I just come back from the long holiday.
I have tried the tcp_send before but maybe I used it inside the callback. I will try it again. Thanks!
It seems I can not get it work. Do you know where is the suitable place to call the tcp_send function. I bring it behind the main_TcpNet (); but it does not work.
Below are my source codes:
/*--------------------------- TCP socket ------------------------------------*/
U16 tcp_callback (U8 soc, U8 evt, U8 *ptr, U16 par) { /* This function is called by the TCP module on TCP event */ /* Check the 'Net_Config.h' for possible events. */ par = par;
if (soc != socket_tcp) { return (0); }
switch (evt) { case TCP_EVT_DATA: /* TCP data frame has arrived, data is located at *par1, */ /* data length is par2. Allocate buffer to send reply. */ procrec(ptr); break;
case TCP_EVT_CONREQ: /* Remote peer requested connect, accept it */ return (1);
case TCP_EVT_CONNECT: /* The TCP socket is connected */ return (1); } return (0); }
void send_data (U8 p2val) { U8 *sendbuf; U8 p2; BOOL success;
/* UDP */ //if (socket_udp != 0) { /* Start Connection */ // sendbuf = udp_get_buf (SENDLEN); // sendbuf[0] = BLINKLED; // sendbuf[1] = p2val; // udp_send (socket_udp, Rem_IP, 1001, sendbuf, SENDLEN); //}
/* TCP */ if (socket_tcp != 0) { /* Start Connection */ p2 = p2val; switch (tcp_get_state(socket_tcp)) { case TCP_STATE_FREE:
case TCP_STATE_CLOSED: tcp_connect (socket_tcp, Rem_IP, PORT_NUM, 0); break;
case TCP_STATE_CONNECT: if (tcp_check_send (socket_tcp) == __TRUE) { sendbuf = tcp_get_buf(SENDLEN); //sendbuf[0] = BLINKLED; sendbuf[0] = p2; sendbuf[1] = 0;
success = tcp_send (socket_tcp, sendbuf, SENDLEN); if (success == __TRUE) { GLCD_DisplayString (1, 1, 1, "PASS"); } //GLCD_DisplayString (1, 1, 1, "CONNECTED");
} break; } } }
int main (void) { /* Main Thread of the TcpNet */
init(); init_display (); init_TcpNet (); SSP0Init();
LED_out (0x07); // CLR,LDAC,REST to High GLCD_Clear (White);
/* Initialize UDP Socket and start listening */ /* socket_udp = udp_get_socket (0, UDP_OPT_SEND_CS | UDP_OPT_CHK_CS, udp_callback); if (socket_udp != 0) { udp_open (socket_udp, PORT_NUM); } */ /* Initialize TCP Socket and start listening */ //socket_tcp = tcp_get_socket (TCP_TYPE_CLIENT_SERVER, 0, 10, tcp_callback); // TCP socket in client & server mode HY socket_tcp = tcp_get_socket (TCP_TYPE_SERVER, 0, 10, tcp_callback); if (socket_tcp != 0) { tcp_listen (socket_tcp, PORT_NUM); }
while (1) { timer_poll (); main_TcpNet (); send_data(65); // This line is for trial to send data 65 ("A") back to PC but still not working ! // Actually I need to return ADC measurement result to the PC. How to do it ? } }
check for the difference.
it's easier if it's posted properly, no?
Oh, yes!
Hi:
1: you don't need the state machine inside the send_data function.
2: are you sure that you have stablished a connection to socket? check wireshark for traffic and make sure that you have the handshake sequence.
3: the function below should work:
void send_data (U8 p2val) {
U8 *ucSendbuf; U32 unIndex;
if (tcp_check_send (tcp_soc))
{ ucSendbuf = tcp_get_buf (p2val);
// copy bytes to send buffer.
for (unIndex = 0; unIndex < p2val ; unIndex++)
{
ucSendbuf[unIndex] = 'A'; }
tcp_send (tcp_soc, ucSendbuf, p2val );
}
} // end function
Hi All ,
I am sorry to bring the difficulty for you to read my source code because I simply use copy & paste without re-align it.
I should improve it next time. Thank for all your kindly help !
Consider using BSD-socket interface. Then the usage of sockets is the same as on PC.
Hi Christian ,
May thanks for your help !
After adapt your code and through wireshark monitor, I have fiexd the problem. I have to add /r/n then the PC can receive the sending string.
No problem