Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

udp packet to tcp packet conversion

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 7611
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 7611
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 7611
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you share what you did for tcp to udp conversion?
 
Saloon Keeper
Posts: 27933
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sameer Javed wrote:can you share what you did for tcp to udp conversion?

It has been a decade. I think they all went home.
 
Marshal
Posts: 4550
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on your needs, socat may be a solution for you.  It is a Linux command line utility which basically inter-works anything to anything else.

For example, to connect to the University of Colorado's time server which listens on TCP port 13 using UDP, you could use:
socat UDP-LISTEN:8000 TCP4:india.colorado.edu:13

Test using NetCat:
Network trace:
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic