• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

create a subfunction for socket client

Hi Experts,

I have wrote a code for socket client as below, it can send a string to server,
The test is ok.

Now I want to create a subfunction, then everytime when want to send a string by socket, just call the function, and pass the string needed to be transferred.

I tried but seems not work.
Since I am a new learner, may you help to write a sample code.
.

Thanks and Regards,

Turbot

#include <stdio.h>
#include "winsock2.h"

void main() {

    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
        printf("Error at WSAStartup()\n");

    // Create a socket.
    SOCKET m_socket;
    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == INVALID_SOCKET ) {
        printf( "Error at socket(): %ld\n", WSAGetLastError() );
        WSACleanup();
        return;
    }

    // Connect to a server.
    sockaddr_in clientService;

    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr( "172.16.2.63" );
    clientService.sin_port = htons( 27015 );

    if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
        printf( "Failed to connect.\n" );
        WSACleanup();
        return;
    }

    // Send and receive data.
    int bytesSent;
    int bytesRecv = SOCKET_ERROR;
    char sendbuf[32] = "Client: ---Sending data.";
    char recvbuf[32] = "";

    bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
    printf( "Bytes Sent: %ld\n", bytesSent );
    bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
    printf( "Bytes Sent: %ld\n", bytesSent );

    while( bytesRecv == SOCKET_ERROR ) {
        bytesRecv = recv( m_socket, recvbuf, 32, 0 );
        if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
            printf( "Connection Closed.\n");
            break;
        }
        if (bytesRecv < 0)
            return;
        printf( "Bytes Recv: %ld\n", bytesRecv );
    }

    return;
/////////////////////////////////////////////////////////////////////// Send 2

}
0
turbot_yu
Asked:
turbot_yu
3 Solutions
 
novitiateCommented:

class Socket
{
public:
      void  send(unsigned char* data, unsigned size) const
      {
          ::send(_socket, data,  size, 0 );
      }
      void operator = (SOCKET & s)
      {
         _socket = s;
      }
private:
      SOCKET _socket;
};

_novi_
0
 
itsmeandnobodyelseCommented:

#include <iostream>
#include <string>
using namespace std

#include "winsock2.h"

void sendString(SOCKET sock, const string& str)
{
   // send with terminating zero character
    int bytesSent = send( sock, str.c_str(), str.length()+1, 0 );
    printf( "Bytes Sent: %ld\n", bytesSent );
}


void main() {

    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
        printf("Error at WSAStartup()\n");

    // Create a socket.
    SOCKET sock;   // prefix m_ should be used for class members only
    sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( sock == INVALID_SOCKET ) {
        printf( "Error at socket(): %ld\n", WSAGetLastError() );
        WSACleanup();
        return;
    }

    // Connect to a server.
    sockaddr_in clientService;

    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr( "172.16.2.63" );
    clientService.sin_port = htons( 27015 );

    if ( connect( sock, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
        printf( "Failed to connect.\n" );
        WSACleanup();
        return;
    }

    // Send and receive data.
    string sendbuf = "Client: ---Sending data.";
    sendData(sock, sendbuf );
    sendData(sock, sendbuf );

    int bytesRecv = SOCKET_ERROR;
    char recvbuf[32] = "";
    while( bytesRecv == SOCKET_ERROR ) {
        bytesRecv = recv( sock, recvbuf, 32, 0 );
        if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
            printf( "Connection Closed.\n");
            break;
        }
        if (bytesRecv < 0)
            return;
        printf( "Bytes Recv: %ld\n", bytesRecv );
    }

    return;
/////////////////////////////////////////////////////////////////////// Send 2

}

Note, you could do similar with the receiving part.

_novi_'s approach to put all socket stuff to class Socket is recommendable.

Regards, Alex



0
 
havman56Commented:
go and get free code from my site fro scokets. Many of them used and works fine.

  see free code

http://micro_mangal.tripod.com
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now