TI-H: Program to test Ethernet<->Calc


[Prev][Next][Index][Thread]

TI-H: Program to test Ethernet<->Calc




>No, it is not true that windows will only compile for windows.

Compile this for anything but windows.  :)


#include <winsock.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int main(int argc, char **argv) {
char szBuf[256];
struct sockaddr_in sin;
WSADATA wsaData;
SOCKET  s;
int rc,i;

 	if (WSAStartup(0x101,&wsaData) == SOCKET_ERROR)
	{
		printf("WSAStartup() failed, rc=%d\n",WSAGetLastError());
		WSACleanup();
		return -1;
	}
	sin.sin_addr.S_un.S_addr = inet_addr("192.168.1.2");
	sin.sin_family = AF_INET;
	sin.sin_port = htons(7);
 	s = socket(AF_INET,SOCK_DGRAM,0);
	if(s<0)
	{
		printf("Couldn't get a socket, rc=%d\n",WSAGetLastError());
		WSACleanup();
		return -1;
	}

	if(connect(s,(struct sockaddr*)&sin,sizeof(sin)) == SOCKET_ERROR)
	{
		printf("connect() failed, rc=%d\n",WSAGetLastError());
		WSACleanup();
		return -1;
	}

	for(i=0;i<5;i++)
	{
		wsprintf(szBuf,"Test %d",i+1);
		rc = send(s,szBuf,strlen(szBuf),0);
		if (rc == SOCKET_ERROR) {
			printf("send() failed,rc=%d\n",WSAGetLastError());
			WSACleanup();
			return -1;
		}
		printf("Sent Data [%s]\n",szBuf);
		rc = recv(s,szBuf,sizeof(szBuf),0 );
		if (rc == SOCKET_ERROR) {
			printf("recv() failed, rc=%d\n",WSAGetLastError());
			closesocket(s);
			WSACleanup();
			return -1;
		}
		if (rc == 0) {
			printf("Server closed connection\n");
			closesocket(s);
			WSACleanup();
			return -1;
		}
		printf("Received %d bytes, data [%s] from the
board\n",rc,szBuf);
	}
	closesocket(s);
	WSACleanup();
}


Follow-Ups: References: