『第
3 楼』:
可以
以下为客户端代码例子:
#include <stdio.h>
#include <string.h>
#include <rtos.h>
#include <net.h>
#include <mem.h>
#include <stdlib.h>
#include <ctype.h>
void main(void)
{
static tcp_Socket s;
char remotbuf[100] = "lanry";
char serverIp[20] = "192.168.0.100"; //服务器IP地址
char AddrBuf[50];
char szRead[512];
DWORD host;
int status;
int rlen = 5;
sock_init(); /*初始化协议栈*/
printf("WATTCP.CFG address is: [%s]\n", inet_ntoa(AddrBuf, gethostid()));
printf("Connecting to %s:%d\n", serverIp, 9104);
host = inet_addr(serverIp); /*服务器端IP*/
if(!tcp_open(&s, 0, host, 9104, NULL)) /*连接服务器*/
{
printf("Unable to open TCPC session\n");
goto sock_err;
}
printf("Wating to be established\n\r");
sock_wait_established(&s, sock_delay, NULL, &status);
printf("Connection is Successful!\n");
/*以下代码用户可以根据需要修改*/
while(1)
{
rt_sleep(10);
tcp_tick(NULL); //给协议栈执行机会
sock_write(&s, (byte *)remotbuf, rlen); //向服务端发送信息
//接收服务端发送来的信息
if(sock_dataready(&s))
{
memset(szRead, 0, 512);
//无阻塞接收数据,另外一个阻塞接收数据函数为sock_read
sock_fastread(&s, (byte*)szRead, sizeof(szRead));
}
}
sock_err:
puts("Unable to connect to the server!\n");
sock_close(&s);
}
|