1

I'm lost. I've been searching the web for days now and I just can't find the answer. I'm more or less a beginner socket programmer but I do understand it.

I want to do the following things:

  1. Create a custom packet (from scratch, setting every value)
  2. Send it

Either Java, C++ or C#. Is there an easy to use library for this or is there a core class that allows me to? I've already tried the Java library jnetpcap but that only gave me errors, even when running the examples and following the installation guide for eclipse.

Any help is much appreciated!

Note: It's for windows

Tim S.
  • 13,597
  • 7
  • 46
  • 72
  • 1
    Do you want to know how to design the packet structure? or you have already decided the packet structure but dont know how to implement it – Jeeva Jul 27 '11 at 07:24
  • 1
    You don't specify how 'custom' your packet has to be but I think you want [this](http://stackoverflow.com/questions/3964013/how-send-raw-ethernet-packet-with-c). – Sem Vanmeenen Jul 27 '11 at 07:26
  • @SemVanmeenen Yeah that's exactly what I need! I still prefer Java or C++ over C# so if you have any c++ or java alternatives that would be good, if not i'll go for this, thanks! – Tim S. Jul 27 '11 at 07:30
  • 1
    I would suggest using Raw Sockets is a fairly advanced topic (something most developers with ten years experience using plain socket won't touch) and at a minimum you should use them after you have very good understanding of how plain sockets work. – Peter Lawrey Jul 27 '11 at 07:42

5 Answers5

4

Java: Socket

http://download.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html

Java: Raw sockets

http://www.savarese.com/software/rocksaw/

C#: TCPClient

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=vs.71%29.aspx

C#: Raw sockets

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

It should be fairly easy looking up examples of all four.

JK.
  • 5,126
  • 1
  • 27
  • 26
  • I've looked into both, but I don't think they'll allow me to build packets from scratch really. Sorry for the unclear question – Tim S. Jul 27 '11 at 07:32
4

jNetPcap is an API based on WinPcap, just install WinPcap and it will work :)

As an alternative, you can try to use other libraries such as Jpcap (for Java) and SharpPcap (for C#)

KevinG
  • 882
  • 3
  • 16
  • 33
Tu Tran
  • 1,957
  • 1
  • 27
  • 50
3

I have used sockets in c++ using winsock in windows and socket bsd in linux.

This was the best guide I found http://beej.us/guide/bgnet/output/html/multipage/index.html

EDIT: Beej's guide has everything: background information, simple examples, advanced topics like data packing and some humour

Leon
  • 1,141
  • 13
  • 25
  • +1 for Beej's guide, it really has everything you need to know. You can buy it as a paperback too. – Valmond Jul 27 '11 at 07:37
  • Beej's guide has everything: background information, simple examples, advanced topics like data packing and some humour – Leon Jul 27 '11 at 07:39
0

Quite easy if u do it java.

for starters. http://download.oracle.com/javase/tutorial/networking/datagrams/clientServer.html

frewper
  • 1,385
  • 6
  • 18
  • 44
0

Tim,

Every packet needs a Fixed Length header followed by the Body and optionally a trailer. The fixed head length header should have details like the Packet Length(Body Length + Trailer Length), Time stamp, Unique Packet Id (Used to break the big packet into multiple small packets).

The receiver will always read the fixed length header first and determine the packet length and read the rest of the packet.

You need to append the size before every variable length elements like name etc.

Example of simple Packet Structure:

Header Size - 1 Byte

Sending details like Name, ID, Sex

Header

[8] - 1 Byte //size (Name Length + Name + Id + Sex)

Body

[5] - 1 Byte //Name Length

[Jeeva] - N Bytes

[1000] - 1 Byte ID

[0] - 1 Byte Sex (0 - Male, 1- Female)

Note: You need to be careful about Endianess. Ask further questions for clarification

Jeeva
  • 4,585
  • 2
  • 32
  • 56