|
You last visited: Today at 01:14
Advertisement
[C++] Send a packet
Discussion on [C++] Send a packet within the C/C++ forum part of the Coders Den category.
12/31/2012, 01:31
|
#1
|
elite*gold: 0
Join Date: Aug 2009
Posts: 36
Received Thanks: 2
|
[C++] Send a packet
Hi... iam not very good with C++, but i made simple pixel bot for little mmorpg game which is working... But i want add to this bot "send packet" function, which will send one simple packet from a game client to the server... The packet what i want to send i found with WPE PRO... My question is, is there anybody, who could help me with sending a one packet? Or send me a tutorial or anything that can help me... Thank you so much and sorry for my english..
|
|
|
12/31/2012, 02:30
|
#2
|
elite*gold: 273
Join Date: Sep 2010
Posts: 1,831
Received Thanks: 786
|
|
|
|
12/31/2012, 12:50
|
#3
|
elite*gold: 9
Join Date: Dec 2009
Posts: 1,071
Received Thanks: 819
|
I once began writing a class for http packets using winsocks... It's probably bad, but it should give you an idea on how to start.
Code:
#pragma comment(lib, "Ws2_32.lib")
#include <Windows.h>
#include <iostream>
#include <string>
#include <sstream>
class packet
{
private:
int Socket;
char* buf;
int size;
sockaddr_in service;
public:
packet(bool pPost, string pLocation, string pHost);
bool Post;
string Location;
string Host;
string Send();
~packet();
};
packet::packet(bool pPost, string pLocation, string pHost)
{
Post = pPost;
Location = pLocation;
Host = pHost;
WSADATA w;
WSAStartup(MAKEWORD(2,2), &w);
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
service.sin_family = AF_INET;
service.sin_port = htons(80);
service.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP
};
string packet::Send()
{
string Request;
if (Post == true) // incomplete
{
Request += "POST ";
}
else
{
Request += "GET ";
}
Request += Location + " HTTP/1.1\r\n";
Request += "Host: " + Host + "\r\n";
Request += "Connection: close\r\n\r\n";
unsigned int bytesSent = 0;
connect(Socket, reinterpret_cast<sockaddr*>(&service), sizeof(service));
do
{
bytesSent += send(Socket, Request.c_str() + bytesSent, Request.size() - bytesSent, 0);
} while(bytesSent < Request.size());
string Response;
char ReceivedBytes[256];
int BytesReceived = 1;
while(BytesReceived > 0)
{
BytesReceived = recv(Socket, ReceivedBytes, sizeof(ReceivedBytes), 0);
Response.append(ReceivedBytes, BytesReceived);
}
return Response;
};
packet::~packet()
{
closesocket(Socket);
WSACleanup();
};
|
|
|
12/31/2012, 12:51
|
#4
|
elite*gold: 1826
Join Date: Mar 2009
Posts: 4,310
Received Thanks: 6,287
|
A better way would be reversing the networkclass of the game and then calling the game-own sendpacket function.
|
|
|
12/31/2012, 13:15
|
#5
|
elite*gold: 0
Join Date: Aug 2009
Posts: 36
Received Thanks: 2
|
Thank you all for helping me... I read about it and its something like "DLL Inject" .. i think ill need take a look for this.
|
|
|
12/31/2012, 22:06
|
#6
|
elite*gold: 42
Join Date: Jun 2008
Posts: 5,425
Received Thanks: 1,888
|
Quote:
Originally Posted by buFFy!
A better way would be reversing the networkclass of the game and then calling the game-own sendpacket function.
|
No.
|
|
|
01/07/2013, 12:09
|
#7
|
elite*gold: 0
Join Date: Dec 2012
Posts: 4
Received Thanks: 1
|
Quote:
Originally Posted by MoepMeep
No.
|
Why not?
I think it would be interesting to know why you think that this way isn't good.
Regards, xINK
|
|
|
01/07/2013, 17:59
|
#8
|
elite*gold: 0
Join Date: Jun 2012
Posts: 187
Received Thanks: 58
|
because you would either need to block the game's networking thread from sending or risk a race condition, which is a recipe for guaranteed disaster.
|
|
|
01/07/2013, 18:26
|
#9
|
elite*gold: 0
Join Date: May 2012
Posts: 868
Received Thanks: 947
|
This is in C#, I will come back with the C++ version in 2 mins.
C++
Code:
int SendPacket(Socket ^s, String ^str) {
try {
str += L"\n\0";
array<Byte> ^packet = Encoding::UTF8->GetBytes(str);
if(s->Connected)
s->Send(packet,str->Length,SocketFlags::None);
}
catch(SocketException ^e) {
return e->ErrorCode;
}
return 1;
|
|
|
01/07/2013, 21:06
|
#10
|
elite*gold: 0
Join Date: May 2009
Posts: 827
Received Thanks: 471
|
Quote:
Originally Posted by chichi011
This is in C#, I will come back with the C++ version in 2 mins.
C++
Code:
int SendPacket(Socket ^s, String ^str) {
try {
str += L"\n\0";
array<Byte> ^packet = Encoding::UTF8->GetBytes(str);
if(s->Connected)
s->Send(packet,str->Length,SocketFlags::None);
}
catch(SocketException ^e) {
return e->ErrorCode;
}
return 1;
|
That's not C++
|
|
|
01/10/2013, 19:03
|
#11
|
elite*gold: 20
Join Date: Jan 2012
Posts: 766
Received Thanks: 645
|
Quote:
Originally Posted by xNopex
That's not C++
|
C++ with .net framework
Mm...
void Send(socket sck, std::string msg)
{
send(sck, msg.c_str(), msg.lenght() + 1, 0);
}
|
|
|
 |
Similar Threads
|
[Help] Conncted, and Packet send
06/15/2012 - SRO Coding Corner - 2 Replies
Please Close this thread :)
|
[C#] SilkroadSecurityApi Send Packet ?
08/07/2011 - SRO Coding Corner - 5 Replies
I looked but could not find any subjects about it.
How can i select monster with SilkroadSecurityApi (7045 Opcode)
Waiting your helps :handsdown:
|
Packet siffer. Cannot send packet back to server [DC]
08/06/2011 - Atlantica Online - 1 Replies
hey guys i wounder how exactly i must send back packets to server ...
I captured various of packets walking , attacking, box opening ones. But if i later send them back to server , server just dc me..
Some one tryed?
|
Play sound via Packet Send?? [Question String Packet]
07/14/2010 - CO2 Private Server - 5 Replies
Yow im trying to figure out why i cant play music with the string packet
What im doin is;
MyChar.Client.SendPacket(Game.Packet.String(MyCha r.UID, 20, Splitter));
My Packet is:
public byte String(long CharId, byte Type, string name)
|
[Question] What Packet to send?
08/05/2009 - CO2 Private Server - 6 Replies
Hello.
I'm in trouble because I don't know what packet to send when I have created a Character.
I tried to send this MessagePacket:
The message appears but don't bring you to the main menu of the client to log into my server
|
All times are GMT +1. The time now is 01:14.
|
|