Thread: winsock

  1. #1
    Registered User
    Join Date
    Dec 2001


    hi i found this code at
    Johnnies winsock tutorial

    #pragma comment (lib,"wsock32.lib")
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    #define NETWORK_ERROR -1
    #define NETWORK_OK     0
    void ReportError(int, const char *);
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
    	WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    	sockVersion = MAKEWORD(1, 1);			// We'd like Winsock version 1.1
    	// We begin by initializing Winsock
    	WSAStartup(sockVersion, &wsaData);
    	// Next, create the listening socket
    	SOCKET listeningSocket;
    	listeningSocket = socket(AF_INET,		// Go over TCP/IP
    			         SOCK_STREAM,   	// This is a stream-oriented socket
    				 IPPROTO_TCP);		// Use TCP rather than UDP
    	if (listeningSocket == INVALID_SOCKET) {
    		nret = WSAGetLastError();		// Get a more detailed error
    		ReportError(nret, "socket()");		// Report the error with our custom function
    		WSACleanup();				// Shutdown Winsock
    		return NETWORK_ERROR;			// Return an error value
    	// Use a SOCKADDR_IN struct to fill in address information
    	SOCKADDR_IN serverInfo;
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_addr.s_addr = INADDR_ANY;         
    	// = inet_addr("");
    	// Since this socket is listening for connections,
    							// any local address will do
    	serverInfo.sin_port = htons(888);		// Convert integer 8888 to network-byte order
    							// and insert into the port field
    	// Bind the socket to our local server address
    	nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr_in));
    	if (nret == SOCKET_ERROR) {
    		nret = WSAGetLastError();
    		ReportError(nret, "bind()");
    		return NETWORK_ERROR;
    	// Make the socket listen
    	nret = listen(listeningSocket, 10);		// Up to 10 connections may wait at any
    							// one time to be accept()'ed
    	if (nret == SOCKET_ERROR) {
    		nret = WSAGetLastError();
    		ReportError(nret, "listen()");
    		return NETWORK_ERROR;
    	// Wait for a client
    	SOCKET theClient;
    	theClient = accept(listeningSocket,
    			   NULL,			// Address of a sockaddr structure (see explanation below)
    			   NULL);			// Address of a variable containing size of sockaddr struct
    	if (theClient == INVALID_SOCKET) {
    		nret = WSAGetLastError();
    		ReportError(nret, "accept()");
    		return NETWORK_ERROR;
    	// Send and receive from the client, and finally,
    	// Shutdown Winsock
    	return NETWORK_OK;
    void ReportError(int errorCode, const char *whichFunc) {
       char errorMsg[92];					// Declare a buffer to hold
    							// the generated error message
       ZeroMemory(errorMsg, 92);				// Automatically NULL-terminate the string
       // The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
       sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
       MessageBox(NULL, errorMsg, "socketIndication", MB_OK);

    i get an error that says call to bind() returned error 1048
    1048 is the error# that u can find


    can anyone see whats wrong?

    btw what i would want to do is an app that sends a text message when u connect to it

  2. #2
    Registered User
    Join Date
    Dec 2001
    hey what's happend to the edit button?

    the erro is 10048 not 1048 sorry

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    What part of it dont you understand?
    Address already in use.
    Typically, only one usage of each socket address (protocol/IP address/port) is permitted. This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that was not closed properly, or one that is still in the process of closing. For server applications that need to bind multiple sockets to the same port number, consider using setsockopt (SO_REUSEADDR). Client applications usually need not call bind at all—connect chooses an unused port automatically. When bind is called with a wildcard address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific address is committed. This could happen with a call to another function later, including connect, listen, WSAConnect, or WSAJoinLeaf.
    It obviously (from the first 2 lines) means the port is probably already doing something. Check you dont already have anything listening on port 888, and that you don't have a firewall or anything stopping applications on that port (or try a port above 1024, like 8090).

    And as to your BTW question, try using the send() or WSASend() function.
    Last edited by nickname_changed; 09-26-2003 at 12:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 07:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Winsock - Where do i start?
    By Brain Cell in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-14-2005, 12:39 PM
  4. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 11:11 AM
  5. Winsock Problem
    By Noxir in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2004, 10:50 AM