Subversion Repositories seema-scanner

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jakw 1
/*****************************************************************************
2
**             TEXAS INSTRUMENTS PROPRIETARY INFORMATION
3
**
4
**  (c) Copyright, Texas Instruments Incorporated, 2012-2013.
5
**      All Rights Reserved.
6
**
7
**  Property of Texas Instruments Incorporated. Restricted Rights -
8
**  Use, duplication, or disclosure is subject to restrictions set
9
**  forth in TI's program license agreement and associated documentation.
10
******************************************************************************/
11
/**
12
*
13
* @file    tcp_client.c
14
*
15
* @brief	TCP implementation using <system supported function > APIs.
16
**/
17
/*****************************************************************************/
18
 
19
 
20
/*Include header files related to TCP/IP communication */
21
//#ifdef __WIN32__
22
//#include "stdafx.h"
23
//#endif
24
 
25
#ifndef WIN32_LEAN_AND_MEAN
26
#define WIN32_LEAN_AND_MEAN
27
#endif
28
 
29
#include <stdio.h>
30
#include <stdlib.h>
31
 
32
#ifdef WIN32
33
    #include <windows.h>
34
    #include <winsock2.h>
35
    #include <ws2tcpip.h>
36
    #include <iphlpapi.h>
37
    #pragma comment(lib, "Ws2_32.lib")
38
#else
39
    #include <netdb.h>
40
    #include <unistd.h>
41
    typedef int SOCKET;
42
    #define INVALID_SOCKET -1
43
    #define SOCKET_ERROR   -1
44
    #define closesocket(s) close(s)
45
#endif
46
 
47
#include "tcp_client.h"
48
 
49
 
50
int TCP_Connect(char *host, unsigned long int port)
51
{
52
 
53
	//int socket = 0;
54
	//1. Declare and Initialize TCP/IP module
55
	//2. Create a socket for connecting to server
56
	//3. Connect to server.
57
	//return socket;
58
 
59
	int iResult;
60
	char charPort[33];
61
 
62
	//Convert the port number into ascii char 
63
    //_ultoa(port,&charPort[0],10);
64
    sprintf(charPort, "%ld", port);
65
 
66
    #ifdef WIN32
67
        WSADATA wsaData;
68
        // Initialize Winsock
69
        iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
70
        if (iResult != 0) {
71
            printf("WSAStartup failed: %d\n", iResult);
72
            return -1;
73
        }
74
    #endif
75
 
76
	//Declare addrinfo object that contains a socketaddr strcuture and initialize these values
77
	struct addrinfo *result = NULL,
78
		*ptr = NULL,
79
        hints = {};
80
    //ZeroMemory( &hints, sizeof(hints) );
81
	hints.ai_family = AF_UNSPEC;
82
	hints.ai_socktype = SOCK_STREAM;
83
	hints.ai_protocol = IPPROTO_TCP;
84
 
85
	// Resolve the server address and port
86
    iResult = getaddrinfo(host, charPort, &hints, &result);
87
	if (iResult != 0) {
88
		printf("getaddrinfo failed: %d\n", iResult);
89
        #ifdef WIN32
90
            WSACleanup();
91
            return -1;
92
        #endif
93
	}
94
 
95
	// Attempt to connect to the first address returned by
96
	// the call to getaddrinfo
97
	ptr=result;
98
 
99
	// Create a SOCKET for connecting to server
100
	SOCKET ConnectSocket = INVALID_SOCKET;
101
 
102
	ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, 
103
		ptr->ai_protocol);
104
 
105
	if (ConnectSocket == INVALID_SOCKET) {
106
        #ifdef WIN32
107
            printf("Error at socket(): %ld\n", WSAGetLastError());
108
            WSACleanup();
109
        #endif
110
        freeaddrinfo(result);
111
		return -1;
112
	}
113
 
114
 
115
	// Connect to server.
116
	iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
117
	if (iResult == SOCKET_ERROR) {
118
		closesocket(ConnectSocket);
119
		ConnectSocket = INVALID_SOCKET;
120
	}
121
 
122
	// Should really try the next address returned by getaddrinfo
123
	// if the connect call failed
124
	// But for this simple example we just free the resources
125
	// returned by getaddrinfo and print an error message
126
 
127
	freeaddrinfo(result);
128
 
129
	if (ConnectSocket == INVALID_SOCKET) {
130
		printf("Unable to connect to server!\n");
131
        #ifdef WIN32
132
            WSACleanup();
133
        #endif
134
		return -1;
135
	}
136
 
137
	return ConnectSocket;
138
}
139
 
140
int TCP_Send(int sock, unsigned char *buffer, int length)
141
{
142
	// Send 'length' number of bytes from buffer via provided
143
	// socket <sock> address
144
	int iResult;
145
	iResult = send(sock,(char*) buffer,length,0);
146
	if (iResult == SOCKET_ERROR) {
147
        #ifdef WIN32
148
            printf("send failed with error: %d\n", WSAGetLastError());
149
        #endif
150
		closesocket(sock);
151
        #ifdef WIN32
152
            WSACleanup();
153
        #endif
154
		return 1;
155
	}
156
 
157
	//printf("Bytes Sent: %ld\n", iResult);
158
	return 0;
159
}
160
 
161
int TCP_Receive(int sock, unsigned char *buffer, int length)
162
{
163
	//Retrieve 'length' number of bytes into 'buffer' from the socket <sock> address
164
	int iResult = 0;
165
 
166
	while(length)
167
	{
168
		iResult = recv(sock, (char*) buffer, length, 0);
169
 
170
		if ( iResult > 0 )
171
			; //printf("Bytes received: %d\n", iResult);
172
		else if ( iResult == 0 )
173
			printf("Connection closed\n");
174
        else {
175
            #ifdef WIN32
176
                printf("recv failed with error: %d\n", WSAGetLastError());
177
            #endif
178
        }
179
		length -= iResult;
180
		buffer += iResult;
181
	}
182
 
183
	return 0;
184
}
185
 
186
int TCP_Disconnect(int sock)
187
{
188
	//free and cleanup socket occupied memory
189
	closesocket(sock);
190
    #ifdef WIN32
191
        WSACleanup();
192
    #endif
193
	return 0;
194
}