Advertisement

send recv

Started by September 29, 2002 01:24 PM
2 comments, last by Pipo DeClown 22 years, 4 months ago
1. DO these work? 2. Do i need to loop RECV or will it keep recv till it recvs a buffer?
  
#include <winsock.h>
#include <stdio.h>
#include <iostream>


int main(int argc, char** argv) {
	WORD version = MAKEWORD(1,1);
	WSADATA wsaData;
	int nRet;

	char* IP=new char[32];
	std::cin>>IP;
	//

	// Start up Winsock as before

	//

	WSAStartup(version, &wsaData);

	
	//

	// Store information about the server

	//

        LPHOSTENT lpHostEntry;
 
        lpHostEntry = gethostbyname(IP);		// Specifying server by its name

        if (lpHostEntry == NULL) {
	   printf("Error at gethostbyaddr()\n");
	   WSACleanup();
	   
	system("pause");
	   return 0;
        }

	//

	// Create the socket

	//

	SOCKET theSocket;

	theSocket = socket(AF_INET,		// Go over TCP/IP

			   SOCK_STREAM,		// Socket type

			   IPPROTO_TCP);	// Protocol

	if (theSocket == INVALID_SOCKET) {
		printf("Error at socket()\n");
		WSACleanup();
		
	system("pause");
		return 0;
	}

	
	//

	// Use SOCKADDR_IN to fill in address information

	//

	SOCKADDR_IN saServer;
	
	saServer.sin_family = AF_INET;
	saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
			  // ^ Address of the server being inserted into the address field

	saServer.sin_port = htons(80);


	//

	// Connect to the server

	//


	nRet = connect(theSocket,
		       (LPSOCKADDR)&saServer,		// Server address

		       sizeof(struct sockaddr));	// Length of address structure

        if (nRet == SOCKET_ERROR) {
	   printf("Error at connect()");
	   WSACleanup();
	   
	system("pause");
	   return 0;
		}else{
			printf("Connected \n");
		}


	// Successfully connected!


	printf("\nReceiveing...\n");
	char* myRBuf = new char[256];
//	strcpy(myRBuf,  "hahahahaahhahahaaa" );

	HRESULT res;

	res  =	recv(theSocket,		// Connected socket

			myRBuf,		// Receive buffer

			sizeof(myRBuf),	// Size of the buffer

			0);

	while( !res )	
    {
		res = recv(theSocket,		// Connected socket

			myRBuf,		// Receive buffer

			sizeof(myRBuf),	// Size of the buffer

			0);
	}
	if(myRBuf)
	printf(myRBuf);

	closesocket(theSocket);

	printf("\nDisconnected \n");
	
	//

	// Shutdown Winsock

	//

	WSACleanup();
	
	system("pause");
	return 0;
}
  
and:
  
#include <winsock.h>
#include <stdio.h>
#include <iostream>


int main(int argc, char** argv) {
	WORD version = MAKEWORD(1,1);
	WSADATA wsaData;
	int nRet;

	char* IP=new char[32];
	std::cin>>IP;
	//

	// Start up Winsock as before

	//

	WSAStartup(version, &wsaData);

	
	//

	// Store information about the server

	//

        LPHOSTENT lpHostEntry;
 
        lpHostEntry = gethostbyname(IP);		// Specifying server by its name

        if (lpHostEntry == NULL) {
	   printf("Error at gethostbyaddr()\n");
	   WSACleanup();
	   
	system("pause");
	   return 0;
        }

	//

	// Create the socket

	//

	SOCKET theSocket;

	theSocket = socket(AF_INET,		// Go over TCP/IP

			   SOCK_STREAM,		// Socket type

			   IPPROTO_TCP);	// Protocol

	if (theSocket == INVALID_SOCKET) {
		printf("Error at socket()\n");
		WSACleanup();
		
	system("pause");
		return 0;
	}

	
	//

	// Use SOCKADDR_IN to fill in address information

	//

	SOCKADDR_IN saServer;
	
	saServer.sin_family = AF_INET;
	saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
			  // ^ Address of the server being inserted into the address field

	saServer.sin_port = htons(80);


	//

	// Connect to the server

	//


	nRet = connect(theSocket,
		       (LPSOCKADDR)&saServer,		// Server address

		       sizeof(struct sockaddr));	// Length of address structure

        if (nRet == SOCKET_ERROR) {
	   printf("Error at connect()");
	   WSACleanup();
	   
	system("pause");
	   return 0;
		}else{
			printf("Connected \n");
		}


	// Successfully connected!


	printf("\nReceiveing...\n");
	char* myRBuf = new char[256];
//	strcpy(myRBuf,  "hahahahaahhahahaaa" );

	HRESULT res;

	res  =	recv(theSocket,		// Connected socket

			myRBuf,		// Receive buffer

			sizeof(myRBuf),	// Size of the buffer

			0);

	while( !res )	
    {
		res = recv(theSocket,		// Connected socket

			myRBuf,		// Receive buffer

			sizeof(myRBuf),	// Size of the buffer

			0);
	}
	if(myRBuf)
	printf(myRBuf);

	closesocket(theSocket);

	printf("\nDisconnected \n");
	
	//

	// Shutdown Winsock

	//

	WSACleanup();
	
	system("pause");
	return 0;
}
  
Plz help!
Use a non-blocking I/O model. If you do use a while-loop and call WSARecv(), make sure you use a thread sychronization event.

Kuphryn
Advertisement
It looks like you''re setting up a standard blocking socket, so recv() will block indefinitely until (barring some catastrophic disaster such as the network subsystem failing) the number of bytes specified is received or the socket is closed. There is no need to loop unless you''re planning to receive multiple chunks of data.
________________________________________________"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
Hi !

For TCP protocol server side mustc accept connection and client connect, alse serve side need bind and listen socket. Read more carefully docs.




This topic is closed to new replies.

Advertisement