+ Reply to Thread
Results 1 to 2 of 2

Thread: What is socket? socket client to send hello world to server?

  1. #1

    What is socket? socket client to send hello world to server?

    What is socket? socket client to send hello world to server in WIN32?

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    Socket is entity that represent the connection between applications.

    Applications can reside on single or another machine or even different network.

    You can create a socket of different type: TCP/IP, UDP, RAW.

    Following is the example that creates a TCP/IP socket using IPv4.

    Code:
    #include <winsock2.h>
    void UseSocket()
    {
            WSADATA objWSADATA ;
    	WSAStartup(MAKEWORD(2, 2), &objWSADATA) ; 
    
    	SOCKET sendSock ;
    	sendSock = socket(AF_INET, SOCK_STREAM, 0) ;
    
    	SOCKADDR_IN local_addr ;
    	memset(&local_addr,0,sizeof(SOCKADDR_IN)) ;
    	local_addr.sin_addr.s_addr = inet_addr("192.168.10.10") ;
    	local_addr.sin_family = AF_INET ;
    	local_addr.sin_port = htons(3567) ;
    
    	connect(sendSock, (LPSOCKADDR)&local_addr, sizeof(local_addr)) ;
    
    	int nReceivedLen = 0 ;
    	int nDataLen = 0 ;
    	int nBytesSent = send(sendSock, "Hello World", strlen("Hello World"), 0) ;
    	char szHello[10] = { 0 } ;
    	if(nReceivedLen = recv(sendSock, (char*) szHello, 10, 0))
    	{
    	}
    
    	WSACleanup() ;	
    }
    You will need to add "Ws2_32.lib" in link property.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts