+ Reply to Thread
Results 1 to 2 of 2

Thread: Creating a server using socket and send data to client...

  1. #1

    Creating a server using socket and send data to client...

    Creating a server using socket and send data to client.

  2. #2
    Server is a entity which can accept connection from client and serve the client request.

    following is very simple server which accept single client connection and send '1' to client.

    Code:
    void UseServer() 
    {
    	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.151.195") ;
    	local_addr.sin_family = AF_INET ;
    	local_addr.sin_port = htons(8000) ;
    
    	bind(sendSock, (struct sockaddr *)&local_addr, sizeof(SOCKADDR_IN));
    
    	listen(sendSock, 10) ;
    
    	SOCKET recvSocket;
    	SOCKADDR_IN sockaddr = { 0 } ;
    	int sockaddrlen = sizeof(SOCKADDR_IN);
    
    	recvSocket = accept(sendSock,(struct sockaddr*)&sockaddr, &sockaddrlen) ;
    	int data = 1 ;
    	send(recvSocket, (char *) &data, sizeof(data), 0) ;
    	
    	WSACleanup() ;
    }

+ 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