Start New Discussion within our Software Development Community

Following is a C++ client(Network Programming) code, I need to translate it to TCL, if anybody can help me, I shall be thankfull ...

//<CLIENT.CPP>
#include <iostream>
#include <conio.h>
#include <string>
#include <winsock2.h>
#include <fstream>

using namespace std;

void main()
{
	string ip	= "127.0.0.1";
	int port	= 6789;
	
	WSADATA wsadat;
	WORD rVersion;
	rVersion = MAKEWORD(2,0);
	
	if(WSAStartup(rVersion, &wsadat) != NO_ERROR)
	{
	cout<<"WSA initialization failed.\n";
	WSACleanup();
	return;
	}

	SOCKET client;
	client = socket(AF_INET, SOCK_STREAM, 0);

	if(client == INVALID_SOCKET)
	{
	cout<<"Error creating socket.\n";
	WSACleanup();
	return;
	}		

	SOCKADDR_IN server;
	server.sin_family		= AF_INET;
	server.sin_addr.s_addr	= inet_addr(ip.c_str());
	server.sin_port			= htons(port);

	if(connect(client, (SOCKADDR*) &server, sizeof(server)) == SOCKET_ERROR)
	{
	cout<<"Connection cannot be established. \n";
	WSACleanup();
	return;
	}

	//send and receive data
	char* buf = "Hello TCP-IP World!\n";//new unsigned char[length];

	send(client, buf, strlen(buf)+1, 0);

	shutdown(client, SD_BOTH);
	closesocket(client);
	WSACleanup();
	return;
}

I think this will do what you want:

proc main {ip port} {
  set connection_to_server [socket $ip $port]
  puts $connection_to_server "Hello TCP-IP World\n"
  close $connection_to_server
  }

main  127.0.0.1  6789

If you need your tcl script to be directly executable from the command line I'll need to know more about your OS.

Oh yeah, you can always get this kind of info from one of
Tcl/Tk 8.5 manual pages
The Tcler's Wiki

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.