Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

udp packet to tcp packet conversion

 
vikas byn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have create an UDP Listening Server {DatagramSocket} and then it convert to tcp packet using its payload then send it on another server like socat utility in linux.
 
Tim Moores
Bartender
Posts: 2955
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errr, UDP is on a lower level that TCP - there's no way to "convert" it to TCP. What, exactly, are you trying to do?
 
vikas byn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Errr, UDP is on a lower level that TCP - there's no way to "convert" it to TCP. What, exactly, are you trying to do?


as of my knowledge, TCP and UDP are at same level.

actually i am using linux socat command like -
socat -d -T30 -ly UDP4-LISTEN:161,fork,bind=172.16.152.5 TCP4:172.16.152.5:161

now I am doing snmpget for 172.16.152.5 and 161 port for this server is forwarded to another server using ssh (jsch.jar)

now we have to implement socat kind of utility in java.
 
Tim Moores
Bartender
Posts: 2955
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, they're on the same level with respect to ISO/OSI, but TCP has features UDP doesn't - so you can't easily convert a UDP stream to a TCP stream. I'm not sure what socat does, but Java doesn't have access to raw TCP or UDP packets anyway; you'd need to resort to a JNI-based solution.
 
vikas byn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
command : socat -d -T30 -ly UDP4-LISTEN:161,fork,bind=172.16.152.5 TCP4:172.16.152.5:161

It is listening on 161 for UDP packet and then it send it as TCP packet on same port and interface.
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some subtle differences between TCP and UDP, and if they don't concern you, then I see no problems converting from UDP to TCP.

For example....

1. UDP is uni-directional. TCP is bi-directional. Since you are going from UDP to TCP, this should not be an issue.

2. UDP is lossy. TCP is not. Again, probably not a concern here.

3. UDP is connection-less. TCP is point to point. Not a problem, if you know that only one sender will be going to a particular address:port; or if you don't care that data may interlace.

4. UDP is datagram based. TCP is "streams" based. Not an issue, if you don't care about the boundaries of the datagrams. Otherwise, you will need to implement some protocol using some sort of delimiter.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Java doesn't have access to raw TCP or UDP packets anyway; you'd need to resort to a JNI-based solution.


Not sure what you mean by "raw TCP or UDP", but you can use the Socket and ServerSocket classes to get to the TCP stack. And use the DatagramSocket or MulticastSocket classes to get to the UDP stack.

Henry
 
Tim Moores
Bartender
Posts: 2955
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Not sure what you mean by "raw TCP or UDP"

It sounded to me like the task at hand requires munging individual TCP/UDP packets (which can't be done in Java); if that's not necessary then I stand corrected.
 
vikas byn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do that successfully. thanks for your suggestions.

for abstract:

I create DatagramSocket (udp server)and then receive DatagramPacket and extract payload using DatagramPacket.getData().
create tcp client socket on same port and write the extracted payload and then write on output stream of client socket and flush out stream.
read inputstream into buffer of client socket.
then DatagramPacket fill with buffer.
DatagramSocket.send(DatagramPacket).

sorry for my bad english.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic