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)
[source]
#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;
};
[/source]
Thanks.
EDIT: the format got messed up from the copy/paste