vxWorks(PPC405ep) 与 windows的Tcp通信

Server端运行于powerPC

//TcpServer.c

#include "vxworks.h"
#include "socklib.h"
#include "inetlib.h"
#include "strlib.h"
#include "hostlib.h"
#include "iolib.h"
#include "stdio.h"


//define
#define MAXLEN 1024
#define SERV_PORT 8000


void NetTest()
{  
    struct sockaddr_in servaddr, cliaddr;
    int cliaddr_len, ret;
    int listenfd, connfd, ret;
    char RecvBuf[MAXLEN];
    
    printf("NetTest Start....\n");
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if(listenfd < 0)

        printf("socket ERROR...\n");

    bzero((char *)&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);

    ret = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    if(ret < 0)
        printf("Bind ERROR...\n");

    ret = listen(listenfd, 20);
    if(ret < 0)
        printf("listen ERROR...\n");
        
    printf("Accepping connections.... \n");
    while(1)
    {
        cliaddr_len = sizeof(cliaddr);
        connfd = accept(listenfd, (struct sockaddr *)&cliaddr, (int *)&cliaddr_len);
        if(connfd < 0)
            printf("accept connections.... \n");

         ret = read(connfd, RecvBuf, sizeof(RecvBuf));
        RecvBuf[ret] = '\0';
        printf("RecvBuf = %s\n", RecvBuf);
        strcpy(RecvBuf, "I am 405 jichao");       
        ret = strlen(RecvBuf);
        printf("RecvBuf size is %d\n", ret);
        while(1)
        {     
            ret = send(connfd, RecvBuf, strlen(RecvBuf), 0);
            printf("Send size is %d\n", ret);
            taskDelay(10);
        }    
        
        close(connfd);
    }
}

Client运行于windows下(VS2008)

//TcpClient.c

#include <stdio.h>
#include "WinSock2.h"
#pragma comment(lib,"ws2_32.lib")

SOCKET tcp_socket;
SOCKADDR_IN tcpAddr;
const int BufLen=1024;
char SendBuf[1024];

#define TCPPORT 8000
#define IPADDR "192.168.1.70"

int main(int argc, char* argv[])
{
 WSADATA wsd;
 unsigned long int Addr;
 int ret, Count = 1;
 
 Addr=inet_addr("192.168.1.70");

 WSAStartup(MAKEWORD(2,2),&wsd);
 tcp_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
 printf("Running in client mode(TCP)...\n");
 tcpAddr.sin_family=AF_INET;
 tcpAddr.sin_port=htons(TCPPORT);
 tcpAddr.sin_addr.s_addr=Addr;
 
 connect(tcp_socket,(LPSOCKADDR)&tcpAddr,sizeof(tcpAddr));

 ZeroMemory(SendBuf,BufLen);
 printf("please input you cmd: TestStart\n");
 fgets(SendBuf,BufLen,stdin);
 send(tcp_socket,SendBuf,BufLen,0);
 
 while (Count)
 {
     ret = recv(tcp_socket, SendBuf, 15, 0);
     //ret = read(tcp_socket, SendBuf, sizeof(SendBuf));
     SendBuf[ret] = '\0';
     printf("PPC405ep  %d return size is %d\t %s\n", Count, ret,  SendBuf);
     Count++;
     if (Count > 10000)
            Count = 1;
 }
 
 closesocket(tcp_socket);
 WSACleanup();  
 return 1;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值