Jump to content
  • Advertisement
Sign in to follow this  
stewypeza

connecting to software via sockets

This topic is 4460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys Using c++ DirectX9: I have network code to connect to a web address which seems to work, but is untested. What i want is code to connect to another program on the same machine so strings can be sent. I have been told i need to create a port to use which they can connect through. I don't know how to do this. My existing network code is shown below.


	WORD sockVersion;
	WSADATA wsaData;
	//int nret; made global
	   
	sockVersion = MAKEWORD(1, 1);
	   
	// Initialize Winsock as before
	WSAStartup(sockVersion, &wsaData);
	   
	// Store information about the server
	LPHOSTENT hostEntry;
	   
	hostEntry = gethostbyname("www.hal-pc.org");     // Specifying the server by its name;
														// another option is gethostbyaddr()

	if (!hostEntry) {
		nret = WSAGetLastError();
		//ReportError(nret, "gethostbyname()");         // Report the error as before
	      
		WSACleanup();
		return NETWORK_ERROR;
	}
	   
	// Create the socket
	//SOCKET theSocket; // made global
	   
	theSocket = socket(AF_INET,                      // Go over TCP/IP
						SOCK_STREAM,                  // This is a stream-oriented socket
						IPPROTO_TCP);                 // Use TCP rather than UDP

	if (theSocket == INVALID_SOCKET) {
		nret = WSAGetLastError();
		//ReportError(nret, "socket()");
	      
		WSACleanup();
		return NETWORK_ERROR;
	}

	// Fill a SOCKADDR_IN struct with address information
	SOCKADDR_IN serverInfo;
	   
	serverInfo.sin_family = AF_INET;
	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);   // See the explanation in the tutorial
	serverInfo.sin_port = htons(80);                 // Change to network-byte order and
														// insert into port field

	// Connect to the server
	nret = connect(theSocket,
					(LPSOCKADDR)&serverInfo,
					sizeof(struct sockaddr));
                  
	if (nret == SOCKET_ERROR) {
		nret = WSAGetLastError();
		//ReportError(nret, "connect()");
	      
		WSACleanup();
		return NETWORK_ERROR;
	}

	// SUCCESSFULLY CONNECTED!


Can this be relatively easily altered to connect to a local host and to create a port. Thank you Stuart

Share this post


Link to post
Share on other sites
Advertisement
Yes, you just need to connect to "localhost". Although you'd be better to change to to this:

WORD sockVersion;
WSADATA wsaData;
//int nret; made global

sockVersion = MAKEWORD(1, 1);

// Initialize Winsock as before
WSAStartup(sockVersion, &wsaData);

// Create the socket
//SOCKET theSocket; // made global

theSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP

if (theSocket == INVALID_SOCKET) {
nret = WSAGetLastError();
//ReportError(nret, "socket()");

WSACleanup();
return NETWORK_ERROR;
}

// Fill a SOCKADDR_IN struct with address information
SOCKADDR_IN serverInfo;

serverInfo.sin_family = AF_INET;
serverInfo.sin_addr = inet_addr("127.0.0.1");
serverInfo.sin_port = htons(80); // Change to network-byte order and
// insert into port field

// Connect to the server
nret = connect(theSocket,
(LPSOCKADDR)&serverInfo,
sizeof(struct sockaddr));

if (nret == SOCKET_ERROR) {
nret = WSAGetLastError();
//ReportError(nret, "connect()");

WSACleanup();
return NETWORK_ERROR;
}

// SUCCESSFULLY CONNECTED!


So you don't have to lookup the hostname. Also, what you have is only half the code - that's the client-side code. You also need more to call bind() and accept() to accept the connection on the other end.

Share this post


Link to post
Share on other sites
Cheers steve thats sounds easy enough.

I am getting one error tho with the line: serverInfo.sin_addr = inet_addr("127.0.0.1");

The error is: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'unsigned long' (or there is no acceptable conversion)

I don't know if im being stupid but I don't know why it is wrong?

cheers

stuart

Share this post


Link to post
Share on other sites
hi istewypeza.

Use serverInfo.sin_addr.s_addr = inet_addr("127.0.0.1");

sin_addr is a struct containing a union, so to access the long member use s_addr.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!