aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes udp packet to tcp packet conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "udp packet to tcp packet conversion" Watch "udp packet to tcp packet conversion" New topic
Author

udp packet to tcp packet conversion

vikas byn
Ranch Hand

Joined: Mar 18, 2011
Posts: 80
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Mar 18, 2011
Posts: 80
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Mar 18, 2011
Posts: 80
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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Mar 18, 2011
Posts: 80
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.
 
 
subject: udp packet to tcp packet conversion