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;
}