C++ TCP client application help required.

Hi, can any body send me the working application of TCP client for windows. I tried to work on the microsoft MSDN winsock one, but it didn't work for me.

I have the server application ready with me now need Client application . can some one help me in this regards.

Thanks in advance
Last edited on
Need more input Stephanie!
this is no good . but ok i take my code . its just TCP client .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <WinSock2.h>

using namespace std;
#pragma comment (lib, "Ws2_32.lib")

void main()
{
	WSADATA data;
	if ( WSAStartup(MAKEWORD(1, 1), &data) )
		return;

	char buffer[100] = "";

	SOCKET sock;
	hostent* host;
	sockaddr_in addr_in;

	if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET )
	{
		WSACleanup();
		return;
	}

	host = gethostbyname( "localhost" ); // change your host here 

	addr_in.sin_family = AF_INET;
	addr_in.sin_port   = htons( 3155 ); // change your port here
	memcpy(&addr_in.sin_addr, *host->h_addr_list, host->h_length);
	memset(addr_in.sin_zero, 0, 8);

	if ( connect(sock, (const sockaddr *)&addr_in, sizeof(sockaddr)) == SOCKET_ERROR )
	{
		WSACleanup();
		closesocket( sock );

		return;
	}

	while ( true )
	{
		recv(sock, buffer, 100, 0); // i think the server send to me \0 and i get it in buffer!!otherwise you must null the end of your receive

		cout << buffer << endl;

		cin.getline(buffer, 99);

		send(sock, buffer, strlen(buffer) + 1, 0); // send buffer + null for server

		if ( !_stricmp(buffer, "close") )
			break;
	}

	WSACleanup();
	closesocket( sock );
}
Last edited on
Are you getting errors when you compile? Are you getting run-time errors?
Topic archived. No new replies allowed.