⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpclient.c

📁 实时操作系统Vxworks下网络测试程序
💻 C
字号:
 
#include "vxWorks.h" 
#include "sockLib.h" 
#include "inetLib.h" 
#include "stdioLib.h" 
#include "strLib.h" 
#include "hostLib.h" 
#include "ioLib.h" 

#define SERVER_PORT_NUM         5001   /* server's port number for bind() */ 
#define SERVER_MAX_CONNECTIONS  4      /* max clients connected at a time */ 

void clientWrite();
void clientRead();
/**************************************************************************** 
* 
* tcpClient - send requests to server over a TCP socket 
* 
* This routine connects over a TCP socket to a server, and sends a 
* user-provided message to the server.  Optionally, this routine 
* waits for the server's reply message. 
* 
* This routine may be invoked as follows: 
*       -> tcpClient "remoteSystem" 
*       Message to send: 
*       Hello out there 
*       Would you like a reply (Y or N): 
*       y 
*       value = 0 = 0x0 
*       -> MESSAGE FROM SERVER: 
*       Server received your message 
* 
* RETURNS: OK, or ERROR if the message could not be sent to the server. 
*/ 
 int                 sFd;           /* socket file descriptor */ 

STATUS tcpClient(char *  serverName     /* name or IP address of server */) 
{ 
	struct sockaddr_in  serverAddr;    /* server's socket address */ 
	int                 sockAddrSize;  /* size of socket address structure */ 
	int                 mlen;          /* length of message */ 

	/* create client's socket */ 
	if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) 
	{ 
		perror ("socket"); 
		return (ERROR); 
	} 

	/* bind not required - port number is dynamic */ 
	/* build server socket address */ 
	sockAddrSize = sizeof (struct sockaddr_in); 
	bzero ((char *) &serverAddr, sockAddrSize); 
	serverAddr.sin_family = AF_INET; 
	serverAddr.sin_port = htons (SERVER_PORT_NUM); 

	if (((serverAddr.sin_addr.s_addr = inet_addr (serverName)) == ERROR) && 
		((serverAddr.sin_addr.s_addr = hostGetByName (serverName)) == ERROR)) 
	{ 
		perror ("unknown server name"); 
		close (sFd); 
		return (ERROR); 
	} 

	/* connect to server */ 
	if (connect (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) 
	{ 
		perror ("connect"); 
		close (sFd); 
		return (ERROR); 
	}
	else
	{
		if ((taskSpawn("tClientWrite", 100, 0, 4096, (FUNCPTR)clientWrite, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)
		{
			logMsg("taskSpawn tClientWrite failed!\n", 0, 0, 0, 0, 0, 0);
		}
		if ((taskSpawn("tClientRead", 100, 0, 4096, (FUNCPTR)clientRead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)
		{
			logMsg("taskSpawn tClientRead failed!\n", 0, 0, 0, 0, 0, 0);
		}
	}
}

void clientWrite()
{
	char str[100]="to server";
	
	while(1)
	{
		if (write (sFd,str,sizeof(str)) == ERROR) 
		{ 
			perror ("write"); 
			close (sFd); 
		} 
		else
		{
			logMsg("toserver=%s\n",str,0,0,0,0,0);
		}
		taskDelay(60);
	}
}

void clientRead()
{
	char str[100];
	while(1)
	{
		if (read (sFd, str, sizeof(str)) < 0) 
		{ 
			perror ("read"); 
			close (sFd); 
		}
		else
		{
			printf ("from SERVER:%s\n", str); 
		}
		taskDelay(60);
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -