匆忙粘贴复制来的, 有些疏漏,,见谅,,,,
wattcp提供的是C函数库,若要编译C++程序,需要在包含头文件前加入如下指令:
#ifdef _cplusplus
extern "C"{
#include "wattcp.h"
}
#endif
必要时需要打开wattcp.h,在所有函数定义前加上上述语句,并重新编译wattcp库,否则便会在链接时发生错误。
dos工控机作为服务器, 监听指定端口:
#include "WATTEST.H"
#include <stdio.h>
#include <dos.h>
static tcp_Socket s;
char send_buff[128]="hello";
//char send_buff[128];
char receive_buff[128];
char remote_ip_string[]="192.168.1.19";
int listen_port=9109;
void main(void){
int loop=0;
int estab_count=0;
int status;
sock_init();
sock_mode(&s,TCP_MODE_ASCII);
printf("_sock_init success !/n");
tcp_listen(&s,listen_port,0,0,NULL,0);
printf("_tcp_listennig at %d !/n",listen_port);
while(!sock_established(&s)) //等待客户端连接
{
tcp_tick(&s);
//rt_sleep(10);
delay(100);
loop++;
if(loop>30){ //每3秒打印一次"waiting sock established..."
estab_count++;
printf("(%d)waiting sock established... /n",estab_count);
loop=0;
}
}
printf("tcp has been sock_established... /n");//已经连接,打印此断点
//tcp_tick(&s);
sock_puts(&s,"connected! /n"); //向服务端发送信息
tcp_tick(&s);
do{
if(sock_dataready(&s)){
//等待client端数据发送完毕,接收并在服务端打印出来
sock_gets(&s,receive_buff,100);
printf("client send : %s",receive_buff);
//向client发送相同的数据一次
sock_puts(&s,receive_buff);
}
}while(tcp_tick(&s));
//sock_established()出错后默认goto此标签
sock_err:
printf("!!! sock_wait_established fff error ! /n");
sock_close(&s);
}
客户端代码:
#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);
}
仅发送:
#include <stdio.h>
#include <tcp.h>
int main()
{
word status;
word port;
longword host;
udp_Socket usock;
//------//
sock_init();
host=resolve("192.168.1.28");
port=8001;
//--- OPEN UDP ---//
udp_open(&usock,0,host,port,NULL);
//--- SEND DATA ---//
sock_write(&usock,"发送成功!!",10);
//--- CLOSE UDP ---//
sock_close(&usock);
sock_exit();
}
仅接收:
int revLen;
char revBuff[1024];
udp_open(&udpSock,localPort,0,0, NULL);
tcp_tick(NULL); //给协议栈执行机会
if ( (revLen = sock_dataready( &udpSock )) != 0 )//检查数据是否到达
{sock_fastread( &udpSock,(byte*)revBuff, revLen );//接受数据}
浙公网安备 33010602011771号