954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Networking

Hello, I was wondering if it was possible to take a string variable or an integer variable and send it to another computer like client/server architecture and also read that data from that same computer. I am thinking that winsock will be ideal for doing this but if anyone knows anything else thats better please tell me. Also if winsock is good for this than can someone point me in the right direction for getting started with it using C++.

Thanks, any help is appreciated.

cam9856
Light Poster
25 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Try this, I haven't read it yet.. but its supposed to be good. :icon_mrgreen:
http://beej.us/guide/bgnet/

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

Just google for "beej".
It's a widely quoted tutorial.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

ok, I have looked through all those tutorials and many other tutorials but it doesnt make sense to me because I am using codeblocks and I keep getting linker errors and all sorts of things. Could someone help me out. I want to be able to take a string variable or an integer variable and send it in a text file to another computer which will be the server. That beej guide isnt helping right now. maybe if someone can post the starting code or explain the linkers for code blocks that would be great. the compiler is GCC or something.

cam9856
Light Poster
25 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

You could try posting your code AND error messages.

Do you expect your doctor to come up with accurate diagnoses when all you can manage to say is "it hurts" ?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

lol, what I am saying is is that I dont no where to start. I mean the linkers it tells me to put in dont even work with the GNU GCC Compiler in codeblocks. Therefore I cant even write any code yet for the networking of the program.

cam9856
Light Poster
25 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
I mean the linkers it tells me to put in dont even work with the GNU GCC Compiler in codeblocks.

Excuse me?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Consider this to be the flavour of the problem rather than an answer (I don't have code blocks on this machine).

In the project settings, there should be a tab to configure the linker. Here you can tell the linker the places where to find other libraries, and the names of those libraries. It may already have the "standard set" of libraries already listed.

What you need to add to that is the name of the winsock library, which IIRC is called ws2_32.lib

Dev-C++ for example calls it libws2_32.a, so feel free to search for likely looking names ending in .lib or .a.

If code blocks follows the standard library naming convention, then libws2_32.a will simply be called ws2_32 on the linker command line.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Thanks i'll give it a shot, and sorry to the messy grammar, I was typing the earlier post quick.

cam9856
Light Poster
25 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

real time example for circular linked list

saravana32
Newbie Poster
1 post since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

Ok, I got the linker working now and I was following the guide on beej but its not working. It gets errors while debugging. Here is my source code, I am not sure what I am doing wrong. But its not the linker this time, I got that to work. Its something in the new winsock code I put in.

#include <iostream>
#include <windows.h>
#include <winsock.h>
using namespace std;

{
WSADATA wsaData; // if this doesn't work
//WSAData wsaData; // then try this instead

if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
int main()
{
system ("TITLE Might and Power");
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << " Sector 2 Entertainment";
Sleep(3000);
system("cls");
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << " Presents";
Sleep(2000);
system("cls");
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << "\n";
cout << " Might and Power";
Sleep(3000);
cout << "\n";
cout << "\n 1) Create Character";
cout << "\n 2) Load Character";
cout << "\n 3) Exit Game";
cout << "\n";
cout << "\n Enter an Option: ";
int choice;
cin >> choice;
if (choice == 1)
CreateCharacter();
if (choice == 2)
LoadCharacter();
}

cam9856
Light Poster
25 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Well your wsa startup is a random bit of code not in any function, and mis-matched braces as well.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

so does the wsa startup have to be in a function like connection or w.e and wat do I do with the braces. Srry if i sound like a noob I am just really lost with this winsock stuff.

cam9856
Light Poster
25 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

How you order the code (all inline in main, lots of functions ) is entirely up to you.

But I'd go with

int main (  ) {
  // do wsa startup here
  // call function to do the entire network session
  // do wsa cleanup here
}
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Ok, thanks I'll give it a shot.

Is that beej guide in C because some of the syntax seems like it is, maybe thats why I am getting errors when debugging aswell.

cam9856
Light Poster
25 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You