Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


#ActualSonicD007

Posted 23 January 2013 - 07:46 PM

Hey everyone,

I'm messing around with sockets and I'm trying to send a simple packet using udp to a server.  As of now, I'm just trying to see if the packet is being sent using wireshark.  However, I don't see anything being sent.  Is there anywhere I messed up here?  (Aside from the system("PAUSE") and pretty much no error checking)

 

 
#include <iostream>
#include <WinSock2.h>
#include <string>
 
#pragma comment(lib, "WS2_32.lib")
using namespace std;
 
int main(int argc, char* argv[])
{
WSADATA info;
WSAStartup(MAKEWORD(2,2), &info);
int client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( client < 0)
{
cout << "Error creating socket" << endl;
system("PAUSE");
return 1;
}
cout << "Socket created" << endl;
 
sockaddr_in clientinfo;
clientinfo.sin_family = AF_INET;
clientinfo.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
clientinfo.sin_port = htons(4511);
 
if (bind(client, (const sockaddr *)&clientinfo, sizeof(clientinfo))< 0)
{
cout << "Error binding socket" << endl;
system("PAUSE");
return 1;
}
cout << "Socket binded." << endl;
 
cout << "You may begin..." << endl;
string message = "";
 
sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
server.sin_port = htons(4510);
while (message != "quit")
{
getline(cin, message);
if (message != "")
{
int i = sendto(client, message.c_str(), strlen(message.c_str() + 1), 0, (sockaddr *)&server, sizeof(server));
cout << "Packet Sent: " << i << " Bytes" << endl;
message = "";
}
}
 
return 0;
};
 
Thanks. 
 
EDIT: the format got messed up from the copy/paste

#2SonicD007

Posted 23 January 2013 - 07:45 PM

Hey everyone,

I'm messing around with sockets and I'm trying to send a simple packet using udp to a server.  As of now, I'm just trying to see if the packet is being sent using wireshark.  However, I don't see anything being sent.  Is there anywhere I messed up here?  (Aside from the system("PAUSE") and pretty much no error checking)

 

 
#include <iostream>
#include <WinSock2.h>
#include <string>
 
#pragma comment(lib, "WS2_32.lib")
using namespace std;
 
int main(int argc, char* argv[])
{
WSADATA info;
WSAStartup(MAKEWORD(2,2), &info);
int client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( client < 0)
{
cout << "Error creating socket" << endl;
system("PAUSE");
return 1;
}
cout << "Socket created" << endl;
 
sockaddr_in clientinfo;
clientinfo.sin_family = AF_INET;
clientinfo.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
clientinfo.sin_port = htons(4511);
 
if (bind(client, (const sockaddr *)&clientinfo, sizeof(clientinfo))< 0)
{
cout << "Error binding socket" << endl;
system("PAUSE");
return 1;
}
cout << "Socket binded." << endl;
 
cout << "You may begin..." << endl;
string message = "";
 
sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
server.sin_port = htons(4510);
while (message != "quit")
{
getline(cin, message);
if (message != "")
{
int i = sendto(client, message.c_str(), strlen(message.c_str() + 1), 0, (sockaddr *)&server, sizeof(server));
cout << "Packet Sent: " << i << " Bytes" << endl;
message = "";
}
}
 
return 0;
};
 
Thanks. 

#1SonicD007

Posted 23 January 2013 - 07:44 PM

Hey everyone,

I'm messing around with sockets and I'm trying to send a simple packet using udp to a server.  As of now, I'm just trying to see if the packet is being sent using wireshark.  However, I don't see anything being sent.  Is there anywhere I messed up here?  (Aside from the system("PAUSE") and pretty much no error checking)

 

 
#include <iostream>
#include <WinSock2.h>
 
using namespace std;
 
int main(int argc, char* argv[])
{
WSADATA startupinfo;
//Create the socket
WSAStartup(MAKEWORD(2,2), &startupinfo);
int server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( server < 0)
{
cout << "Error creating socket" << endl;
system("PAUSE");
return 1;
}
cout << "Socket created" << endl;
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
address.sin_port = htons(4510);
 
if (bind(server, (const sockaddr *)&address, sizeof(address)) < 0)
{
cout << "Error binding to port" << endl;
system("PAUSE");
return 1;
}
cout << "Binded to port 4510" << endl;
char buffer[256];
sockaddr from;
bool quit = false;
while (quit == false)
{
int bytesReceived = recvfrom(server, buffer, sizeof(buffer), 0, &from, (int*)sizeof(from));
if (bytesReceived > 0)
cout << "Bytes Received: " << bytesReceived << endl;
else
{
int i = WSAGetLastError();
if (i != 10014)
cout << WSAGetLastError() << endl;
}
}
closesocket(server);
cout << "Socket closed" << endl;
WSACleanup();
system("PAUSE");
return 0;
};
 
Thanks. 

PARTNERS