The Asylum Private Messages Options Search Blogs Images Chat Cam Portals Calendar FAQ's Join  
Asylum Forums : Powered by vBulletin version 2.2.8 Asylum Forums > WIT - Whore Institute of Technology > TCP/IP
  Last Thread   Next Thread
Author
Thread [new thread]    [post reply]
Mu
I'm a Fiber Monkey!

Registered: Sep 2000
Location: Stuttgart, Germany
Posts: 49

Post TCP/IP

Any one know where I can find some decent C++ tuts for sending stuff over TCP networks??
I know nothing!

Report this post to a moderator | IP: Logged

Old Post 09-30-2000 12:46 AM
Mu is offline Click Here to See the Profile for Mu Click here to Send Mu a Private Message Visit Mu's homepage! Find more posts by Mu Add Mu to your buddy list [P] Edit/Delete Message Reply w/Quote
Pangloss
feu follet

Registered: Aug 2000
Location: 54.60°N 5.70°W
Posts: 1950

Post

Steps of a TCP Server


  • socket() - Get a socket
  • bind() - bind to the socket
  • listen() - listen for traffic on the socket
  • accept() - accept incoming connections on the socket
  • recv() - receive incoming traffic send() -
  • send traffic to connection


Steps of a TCP Client

  • socket() - Get a socket
  • connect() - Connect to a server
  • send() - send traffic to server
  • recv() - receive traffic from server


Sample Code:

//---------------------------------------------------------------------------
#include
#include
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// We should be checking for errors in the code - however I am not for speeds sake
SOCKET s, incoming;
WSADATA wsaData;
SOCKADDR_IN sockAddr, incomingSockAddr;
char buffer[2048];
int length;


// Load the WINSOCK.DLL and initialize it
if(WSAStartup(0x0101, &wsaData))
{
Application->MessageBox("Could not load WINSOCK.DLL", "ERROR", MB_OK);
return;
}

// We use SOCK_STREAM for TCP. SOCK_DGRAM is for UDP
s = socket(PF_INET, SOCK_STREAM, 0);

// Define the socket address
sockAddr.sin_family = PF_INET;
// Port number of HTTP is 80
sockAddr.sin_port = htons(80);
// I'll don't care what address - any address will do
sockAddr.sin_addr.s_addr = INADDR_ANY;

// associate local address with socket
bind(s,(SOCKADDR *)&sockAddr, sizeof(SOCKADDR_IN));

// Listen for an incoming connection
listen(s, SOMAXCONN); // SOMAXCONN means the maximun reasonable amount

while(Button2->Enabled)
{
// accept a connection on the socket
length = sizeof(SOCKADDR_IN);
incoming = accept(s, (SOCKADDR *)&incomingSockAddr, &length);

while(length != 0 && Button2->Enabled)
{
// receive data from the socket
length = recv (incoming, buffer, 2048, 0);
Memo1->Lines->Add(buffer);
send(incoming, "hello\r\n", 7, 0);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Button2->Enabled = FALSE;
}
//---------------------------------------------------------------------------

------------------
There is no music any more.
Music was assassinated on the 8th December 1980

Report this post to a moderator | IP: Logged

Old Post 09-30-2000 12:57 AM
Pangloss is offline Click Here to See the Profile for Pangloss Click here to Send Pangloss a Private Message Find more posts by Pangloss Add Pangloss to your buddy list [P] Edit/Delete Message Reply w/Quote
Pangloss
feu follet

Registered: Aug 2000
Location: 54.60°N 5.70°W
Posts: 1950

Post

Dingle would know more about it though. All of the above was routed straight through my ass, most likely, so wait until he checks it out for you. I'm drunk atm, and when I'm in this state I think I am the God of all techie shit. I'll be sober tomorrow and tell you that the above will never work ... probably

------------------
There is no music any more.
Music was assassinated on the 8th December 1980

Report this post to a moderator | IP: Logged

Old Post 09-30-2000 12:59 AM
Pangloss is offline Click Here to See the Profile for Pangloss Click here to Send Pangloss a Private Message Find more posts by Pangloss Add Pangloss to your buddy list [P] Edit/Delete Message Reply w/Quote
Method Dan
Fluffy Bunny

Registered: Aug 2000
Location: San Francisco Bay Area, CA
Posts: 458

Post

Could you be a prince and try to dig an answer to my thread out of there? You seem to have more technical knowledge in your ass than I have in my head. (And I fancy myself a geek... tsk).

------------------
-Method Dan

Report this post to a moderator | IP: Logged

Old Post 09-30-2000 05:45 AM
Method Dan is offline Click Here to See the Profile for Method Dan Click here to Send Method Dan a Private Message Visit Method Dan's homepage! Find more posts by Method Dan Add Method Dan to your buddy list [P] Edit/Delete Message Reply w/Quote
missphinx
Edgy the Budgie

Registered: Jul 2000
Location:
Posts: 5526

Post

Pangloss et al., here's Method Dan's post, for your convenience:
http://www.asylumwhores.com/ubb/Forum2/HTML/002521.html

Report this post to a moderator | IP: Logged

Old Post 09-30-2000 05:50 AM
missphinx is offline Click Here to See the Profile for missphinx Click here to Send missphinx a Private Message Find more posts by missphinx Add missphinx to your buddy list [P] Edit/Delete Message Reply w/Quote
Mu
I'm a Fiber Monkey!

Registered: Sep 2000
Location: Stuttgart, Germany
Posts: 49

Thumbs up

Thanks Pangloss, I didn't realy understand the stuff that came out of your ass - is there any chance some one has some simpler code - prefrably some that is intelligible (when read by me).


------------------

"My mumma said that do get things done...."

Report this post to a moderator | IP: Logged

Old Post 09-30-2000 08:15 PM
Mu is offline Click Here to See the Profile for Mu Click here to Send Mu a Private Message Visit Mu's homepage! Find more posts by Mu Add Mu to your buddy list [P] Edit/Delete Message Reply w/Quote
lo-key
Administrator

Registered: Jul 2000
Location: Clevelaund Ohissy
Posts: 722

Post

There is also a good book for programming C over IP networks. I can't think of who writes it but I will drop you a line when I find it again.

Report this post to a moderator | IP: Logged

Old Post 10-01-2000 01:03 AM
lo-key is offline Click Here to See the Profile for lo-key Click here to Send lo-key a Private Message Find more posts by lo-key Add lo-key to your buddy list [P] Edit/Delete Message Reply w/Quote
aminal
incomplete

Registered: Jul 2000
Location: Erehwon
Posts: 7538

Post

quote:
Originally posted by Mu:
Thanks Pangloss, I didn't realy understand the stuff that came out of your ass


Dont worry - do what the rest of us do... when stuff comes out of his arse just nod and smile in the right places... it makes Pang feel all impotent(sic)



------------------
a /\/\ i n a l

"KRACKK! goes the hymen"

Report this post to a moderator | IP: Logged

Old Post 10-02-2000 12:22 AM
aminal is offline Click Here to See the Profile for aminal Click here to Send aminal a Private Message Find more posts by aminal Add aminal to your buddy list [P] Edit/Delete Message Reply w/Quote
Dingle
Prison Rapemaster

Registered: Jul 2000
Location: Minneapolis, MN
Posts: 10231

Post

Mu, i havnt touched sockets yet so i cant help you, but if you post it at http://www.codeguru.com i guarantee youll get mucho advice.

Heres some stuff i dug out of MSDN that should be helpful (note there is TONS of info in MSDN, if you have VC++ but dont have MSDN you need to get it, or its online at http://msdn.microsoft.com i believe)

A working CSocket example:

The following example illustrates how you use the archive to send and receive data via CSocket objects. The example is designed so that two instances of the application (on the same machine or on different machines on the network) exchange data. One instance sends data, which the other instance receives and acknowledges. Either application can initiate an exchange — either can act as server or as client to the other application. The following function is defined in the application’s view class:


code:
void CBlabberView::PacketSerialize(long nPackets, CArchive& arData, CArchive& arAck) { if (arData.IsStoring()) { CString strText; for(int p = 0; p < nPackets; p++) { BYTE bValue = (BYTE)(rand()%256); WORD nCopies = (WORD)(rand()%32000); // send header information arData << bValue << nCopies; for(int c = 0; c < nCopies; c++) { // send data arData << bValue; } Text.Format("Received Packet %d of %d (Value=%d,Copies=%d)",p,nPackets,(int)bValue,nCopies); // send receipt string arData << strText; arData.Flush(); // receive acknowledgment arAck >> strText; // display it DisplayMessage(strText); } } else { CString strText; BYTE bCheck; WORD nCopies; for(int p = 0; p < nPackets; p++) { // receive header information arData >> bCheck >> nCopies; for(int c = 0; c < nCopies; c++) { // receive data arData >> bValue; if (nCheck != bValue) AfxMessageBox("Packet Failure"); } } // receive receipt string and display it arData >> strText; DisplayMessage(strText); Text.Format("Sent Packet %d of %d (Value=%d,Copies=%d)",p,nPackets,(int)bValue,nCopies); // send acknowledgment arAck << strText; arAck.Flush(); } }



The most important thing about this example is that its structure parallels that of an MFC Serialize function. The PacketSerialize member function consists of an if statement with an else clause. The function receives two CArchive references as parameters: arData and arAck. If the arData archive object is set for storing (sending), the if branch executes; otherwise, if arData is set for loading (receiving) the function takes the else branch. For more information about serialization in MFC, see the article Serialization.

Note The arAck archive object is assumed to be the opposite of arData. If arData is for sending, arAck receives, and vice versa.

For sending, the example function loops for a specified number of times, each time generating some random data for demonstration purposes. Your application would obtain real data from some source, such as a file. The arData archive’s insertion operator (<< ) is used to send a stream of three consecutive chunks of data:

A “header” that specifies the nature of the data (in this case, the value of the bValue variable and how many copies will be sent).
Both items are generated randomly for this example.

The specified number of copies of the data.
The inner for loop sends bValue the specified number of times.

A string called strText that the receiver displays to its user.
For receiving, the function operates similarly, except that it uses the archive’s extraction operator (>> ) to get data from the archive. The receiving application verifies the data it receives, displays the final “Received” message, then sends back a message that says “Sent” for the sending application to display.

Don’t be confused by the word “Received” in the message sent in the strText variable. In this communications model, it’s for display at the other end of the communication, so it specifies to the receiving user that a certain number of packets of data have been received. The receiver replies with a similar string that says “Sent” — for display on the original sender’s screen. Receipt of both strings indicates that successful communication has occurred.


----

sorry i coulndt help you more!

Report this post to a moderator | IP: Logged

Old Post 10-04-2000 03:53 AM
Dingle is offline Click Here to See the Profile for Dingle Click here to Send Dingle a Private Message Find more posts by Dingle Add Dingle to your buddy list [P] Edit/Delete Message Reply w/Quote
Mu
I'm a Fiber Monkey!

Registered: Sep 2000
Location: Stuttgart, Germany
Posts: 49

Post

That was realy clear, thank you Dingle - When i get round to it ill try this stuff out and tell you now it goes, im working on OpenGL stuff at the moment though.

Report this post to a moderator | IP: Logged

Old Post 10-05-2000 01:35 AM
Mu is offline Click Here to See the Profile for Mu Click here to Send Mu a Private Message Visit Mu's homepage! Find more posts by Mu Add Mu to your buddy list [P] Edit/Delete Message Reply w/Quote
Dingle
Prison Rapemaster

Registered: Jul 2000
Location: Minneapolis, MN
Posts: 10231

Post

what are you working on MU, if i may ask?

Report this post to a moderator | IP: Logged

Old Post 10-09-2000 07:21 AM
Dingle is offline Click Here to See the Profile for Dingle Click here to Send Dingle a Private Message Find more posts by Dingle Add Dingle to your buddy list [P] Edit/Delete Message Reply w/Quote
Mu
I'm a Fiber Monkey!

Registered: Sep 2000
Location: Stuttgart, Germany
Posts: 49

Post

quote:
Originally posted by Dingle:
what are you working on MU, if i may ask?


Well Ive been learning OpenGL recently and this coupled with my belief that Im a l337 programmer make me think I can create something like a game - so ive been finding information on octrees and things like that.

p.s. has anyone else ever used Opnet?

Report this post to a moderator | IP: Logged

Old Post 10-11-2000 12:53 AM
Mu is offline Click Here to See the Profile for Mu Click here to Send Mu a Private Message Visit Mu's homepage! Find more posts by Mu Add Mu to your buddy list [P] Edit/Delete Message Reply w/Quote
All times are GMT. The time now is 01:46 PM. Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread

Forum Jump:
 

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

< Contact Us - The Asylum >

Powered by: vBulletin Version 3.0.6
Copyright ©2000 - 2002, Jelsoft Enterprises Limited.
Copyright © 2000- Imaginet Inc.
[Legal Notice] | [Privacy Policy] | [Site Index]