[Contiki-developers] example tcp connection program

瀏覽次數:55 次
跳到第一則未讀訊息

Andreas Krumholz

未讀,
2010年12月26日 上午11:13:052010/12/26
收件者:contiki-d...@lists.sourceforge.net
hi,

I'm new to contiki. I tried for several weeks now to write a simple
program to connect a client with a server via tcp. my problem is that
my client doesnt send any packages. my question is can anyone post me
a real simple client server program with a functional tcp or explain
to me my mistake.
my code for the client is:

#include <string.h>
#include <stdio.h>
#include "contiki-net.h"

static struct psock csocket;
static char buffer[50];
static uip_ipaddr_t hostaddr;
static uip_ipaddr_t serveraddr;
static struct uip_conn *client_con;


static PT_THREAD(handle_connection(struct psock *p))
{

PSOCK_BEGIN(p);

PSOCK_SEND_STR(p, "Welcome, please type something and press return.\n");

PSOCK_READTO(p, '\n');

PSOCK_SEND_STR(p, "Got the following data: ");
PSOCK_SEND(p, (uint8_t*)buffer, PSOCK_DATALEN(p));
PSOCK_SEND_STR(p, "Good bye!\r\n");

PSOCK_CLOSE(p);

PSOCK_END(p);
}

PROCESS(client_process, "Test Client");
AUTOSTART_PROCESSES(&client_process);

PROCESS_THREAD(client_process, ev, data) {

PROCESS_BEGIN();

uip_ipaddr(&hostaddr, 192, 168, 1, 2);
uip_sethostaddr(&hostaddr);

uip_ipaddr(&serveraddr, 192, 168, 1, 1);

while(1) {


client_con = tcp_connect(&serveraddr, UIP_HTONS(50000), NULL);

PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);

if(uip_connected()) {
PSOCK_INIT(&csocket, (uint8_t*)buffer, sizeof(buffer));
handle_connection(&csocket);

}
}
PROCESS_END();
}


and the server:

#include <string.h>
#include <stdio.h>
#include "contiki-net.h"

static struct psock ssocket;
static char buffer[50];
static uip_ipaddr_t hostaddr;

static PT_THREAD(handle_connection(struct psock *p))
{

PSOCK_BEGIN(p);

PSOCK_SEND_STR(p, "Welcome, please type something and press return.\n");

PSOCK_READTO(p, '\n');

PSOCK_SEND_STR(p, "Got the following data: ");
PSOCK_SEND(p, (uint8_t*)buffer, PSOCK_DATALEN(p));
PSOCK_SEND_STR(p, "Good bye!\r\n");

PSOCK_CLOSE(p);

PSOCK_END(p);
}


PROCESS(server_process, "Test Server");
AUTOSTART_PROCESSES(&server_process);

PROCESS_THREAD(server_process, ev, data) {

PROCESS_BEGIN();

uip_ipaddr(&hostaddr, 192, 168, 1, 1);
uip_sethostaddr(&hostaddr);

tcp_listen(UIP_HTONS(50000));

while(1) {


PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
if(uip_connected()) {

PSOCK_INIT(&ssocket, (uint8_t*)buffer, sizeof(buffer));
while(!(uip_aborted() || uip_closed())) {

PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
handle_connection(&ssocket);
}
}
}
PROCESS_END();
}


thanks!

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Contiki-developers mailing list
Contiki-d...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/contiki-developers

Hussam Abdul Khalek

未讀,
2021年10月23日 清晨7:50:152021/10/23
收件者:osdeve.mirror.rtos.Contiki-developers
Hello Andreas,

I know this is quite a while back, but were you able to figure this out? I am trying to do the same thing to no avail.

I would appreciate some feedback.

Thank you.

Kind regards,
Hussam

回覆所有人
回覆作者
轉寄
0 則新訊息