Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have changed the code, which call the WSAGetLastError, but still the output is "Bind failed:10048 Press any key to continue...", I donot know how to specify this 10048 error, how can i do it? I list the code example here,many thanks.

#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
void main() {

    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
        printf("Error at WSAStartup()\n");
 
    SOCKET server;
    server = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    if ( server == INVALID_SOCKET ) {
        printf( "Error at socket(): %ld\n", WSAGetLastError() );
        WSACleanup();
        return;
    }
  
    sockaddr_in service;
    service.sin_family = AF_INET;
    service.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    service.sin_port = htons( 27015 );
	//
	iResult = bind( server, (SOCKADDR *) &service,sizeof(service));
if (iResult == SOCKET_ERROR) {
    printf("bind failed: %d\n", WSAGetLastError());
    closesocket(server);
	WSACleanup();
    return;
}

    /*if ( bind( server, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
        printf( "bind() failed.\n" );
        closesocket(server); 
        return;
    }*/
    
 
    if ( listen( server, 1 ) == SOCKET_ERROR )
        printf( "Error listening on socket.\n");
  
    SOCKET AcceptSocket;
    printf( "Waiting for a client to connect...\n" );
    while (1) {
        AcceptSocket = SOCKET_ERROR;
        while ( AcceptSocket == SOCKET_ERROR ) {
            AcceptSocket = accept( server, NULL, NULL );
        }
        printf( "Client Connected.\n");
        server = AcceptSocket; 
        break;
    }
    
 
    int bytesSent;
    int bytesRecv=SOCKET_ERROR;
    char sendbuf[32] = "Server: Sending Data.";
    char recvbuf[32] = "";
    
    bytesRecv = recv( server, recvbuf, 32, 0 );
    printf( "Bytes Recv: %ld\n", bytesRecv );
    
    bytesSent = send( server, sendbuf, strlen(sendbuf), 0 );
    printf( "Bytes Sent: %ld\n", bytesSent );
    return;
}
Posted
Updated 18-Oct-10 16:51pm
v2

That particular error would imply that there is already something bound to the 127.0.0.1:27015 IP:Port combination if you run netstat /a from a command prompt you should be able to confirm if this is the case. If this is the case choose a different port number.

Winsock Error WSAEADDRINUSE - Error 10048[^]
 
Share this answer
 
It could not bind to address 27.0.0.1:27015 no listening sockets available.
Check this link for more reference.
http://www.win32developer.com/tutorial/winsock/winsock_tutorial_5.shtm[^]
 
Share this answer
 
hi..
in place of "127.0.0.1" you should write the ip address of your server,
as 127.0.0.1 is a loopback address..

Hope this will help you..
tc..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900