Thread: c++ networking

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    c++ networking

    I have a pretty well understanding of C++, but I have never ventured to learning how to network with c++ - setup a client/server, do basic networking commands such as ping, etc. What is a good library to use if I want my programs to compile on Windows and Linux? Also, are there any good books/tutorials that go along with that library that could help me out?

    Thanks

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    I just started looking into this the other day to
    and i have been programming for a while, so if you
    dont understand basic of c/c++ thne you may want to wait
    here is a list of some good sites

    and here is some code which i just got around
    to writing the other day, however it is for windows

    client :

    Code:
    #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 hInstance, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
    {
    	WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    	
    	sockVersion = MAKEWORD(1, 1);
    
    	WSAStartup(sockVersion, &wsaData);
    
    	LPHOSTENT hostEntry;
    	in_addr iaHost;
    	iaHost.s_addr = inet_addr("127.0.0.1");
    
    
    	hostEntry = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);
    	//hostEntry = gethostbyname("www.hal-pc.org"); //gethostbyaddr();
    
    	if(!hostEntry)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "gethostbyname()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    	SOCKET theSocket;
    
    	theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    	if(theSocket == INVALID_SOCKET)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "socket()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    	SOCKADDR_IN serverInfo;
    
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    	serverInfo.sin_port = htons(88);
    
    	nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
    
    	if(nret == SOCKET_ERROR)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "connect()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    	
    	char buffer[256];
    	//char *buffer = new char[256];
    
    	ZeroMemory(buffer, 256);
    	strcpy(buffer, "Your a faggot :).");
    
    	nret = send(theSocket, buffer, strlen(buffer), 0);
    
    	if(nret == SOCKET_ERROR)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "send()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    	else
    	{
    		char msges[100];
    		ZeroMemory(msges, 100);
    		sprintf(msges, "Sent %i bytes!", nret);
    		MessageBox(NULL, msges, "SentIndication", MB_OK);	
    	}
    
    	closesocket(theSocket);
    	WSACleanup();
    	return NETWORK_OK;
    }
    
    void ReportError(int errorCode, const char *whichFunc)
    {
    	char errorMsg[92];
    	ZeroMemory(errorMsg, 92);
    	sprintf(errorMsg, "Call to %s returned error %d!", (char*)whichFunc, errorCode);
    	MessageBox(NULL, errorMsg, "socketIndication", MB_OK);
    }


    server :

    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <string>
    #include <stdio.h>
    using namespace std;
    #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);
    
    	WSAStartup(sockVersion, &wsaData);
    
    	SOCKET listeningSocket;
    
    	listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    	if(listeningSocket == INVALID_SOCKET)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "socket()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	SOCKADDR_IN serverInfo;
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_addr.s_addr = INADDR_ANY;
    	serverInfo.sin_port = htons(88);
    
    	nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
    
    	if(nret == SOCKET_ERROR)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "bind()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    	nret = listen(listeningSocket, 10);
    
    	if(nret == SOCKET_ERROR)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "listen()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    	SOCKET theClient;
    	theClient = accept(listeningSocket, NULL, NULL);
    
    	if(theClient == INVALID_SOCKET)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "accept()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    	string got, got1, all, all1;
    	char buffer[256];
    	nret = recv(theClient, buffer, 256, 0);
    	got = "You recieved the following message : ";
    	got1 = buffer;
    	all1 = got + got1;
    	all = all1.substr(0, ((all1.find_first_of(".?!", 0))+1));
    	
    	if(nret == SOCKET_ERROR)
    	{
    		nret = WSAGetLastError();
    		ReportError(nret, "recv()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    	else
    	{
    		MessageBox(NULL, all.c_str(), "RecvIndication", MB_OK);
    	}
    
    	closesocket(theClient);
    	closesocket(listeningSocket);
    
    	WSACleanup();
    
    	return NETWORK_OK;
    }
    
    void ReportError(int errorCode, const char *whichFunc)
    {
    	char errorMsg[92];
    	ZeroMemory(errorMsg, 92);
    	sprintf(errorMsg, "Call to %s returned error %d!", (char*)whichFunc, errorCode);
    	MessageBox(NULL, errorMsg, "socketIndication", MB_OK);
    }
    i accomplished thsi code using this site

  3. #3
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    http://www.madwizard.org/view.php?pa...tents&lang=cpp

    Also useful for learning the WinSock API, and focuses more on C++, whereas Johnnie's uses C. Good stuff.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    There probably isn't really much of a good reason to do networking in C/C++. The actual networking does not require much CPU since it's IO Bound so "It's faster" is not much of a valid reason and the language does not provide much useful networking tools.

    For high performance networking apps I'd suggest Erlang.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    2
    Thanks for all of the replies, I'll look into it (and try to be an active member on this board)

  6. #6
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    There probably isn't really much of a good reason to do networking in C/C++. The actual networking does not require much CPU since it's IO Bound so "It's faster" is not much of a valid reason and the language does not provide much useful networking tools.
    Why would you say this? There are plenty of good networked apps written in C/C++. It is true they don't have an official and universal "use this for all networking needs" library, however C/C++ are quite powerful in their own right, and can do networking quite well. I really think it is bad to say "don't network in C/C++, it's bad" because the language itself is somewhat of a generalist, it can do pretty much everything, and do it quite fast, however, it may not be the fastest in everything.

    Erlang seems designed around networking for sure, it might be good if you want to write and go on the fly. I do not have much experience with the language so I would encourage you to check it out should you want to learn something else, or at least experience a braoder range of programming ideas.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Given that all programming languages are equivalent in terms of computability saying that X has been written in C/C++ is not much of a useful statement. What a language such as Common Lisp or Erlang does has to be written somewhere so you could write those same tools in C/C++. However a more useful statement would be how much work you have to do in order to get the equivalent functionality. For instance I can write a program which decodes a fairly complex binary format quite quickly in erlang because it has a lot of tools to help do that.
    I think it is important to realize this does not mean something such as C or C++ are useless, I am not saying that. There are good reasons why languages like Erlang and Python and Ruby attempt to make it easy to communicate with something written in C or C++. However I don't think the strengths a language like C or C++ have to offer apply to networking. Applications tend to take longer to get to a degree of usability and stability than some other languages.
    If you can offer a nice list of reasons why you would prefer C/C++ then I'd be glad to hear them and offer my opinion on them.

  8. #8
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    What I was hearing was, "Don't use C/C++, its useless for this particular reason", now it looks like that is not what you meant, but that was my intepretation.

    There are plenty of reasons why you might use C/C++, such as for integration with an application, which needs the power of C/C++ behind it.

    There are more, but really I suppose its my personal preference, I am just advocating that he keeps his eyes open. I suppose I did a bad job of presenting that opinion as well.

    There is never a "this language is right for this purpose", except for a few occasions where it is impossible to do something in one language.

    Anyway, its a losing argument of what is and isn't the best, because it is truely a large part opinion based.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    I would agree that there is no "this language is right for this purpose", but "this language is better for this purpose" is probably a failry valid statement once you define what 'better' means.

  10. #10
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Very true I suppose I shouldn't have jumped on that, just sometimes people make blanket statements without qualifying, which I missed. In any event erlang looks like an interesting language to use. I would encourage ThinkGeekness to explore as many routes as he can, I do not know if this is a time-based project or what, but if he has the time, to delve into it as much as he can, its quite interesting to me anyway.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Yes, learning as many languages as you can is a good idea. Erlang is probably one of my favorites at the moment. I find implementing things in it to be qutie clean, mostly because it is a single assignment language so functions need to be short generally, which i find nice. It does obviously have some annoyances and limitations, but they all do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. networking comfirmation
    By rEtard in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-20-2005, 03:59 PM
  2. Uhh networking help?!?
    By Shadow12345 in forum Tech Board
    Replies: 13
    Last Post: 09-29-2002, 11:39 PM
  3. Beyond MFC : COM || Networking
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2002, 04:28 PM
  4. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM