Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simulating TCP via UDP

800443Nov 28 2008 — edited Nov 30 2008
Dear All I want to write a frame work to act as reliable data transfer protocol ( RDT1.0) without ACK and NACK using UDP ,

can some one please help me with the code?
I have come to some steps but now I stack here, can some one please push me forward?
// importing packages
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.channels.Selector;


public class Ex5 {
	// declaring udp socket
	DatagramSocket socket ;
	// declaring receive and send packets
	DatagramPacket rcv_pkt;
	DatagramPacket send_pkt;
	// declaring send and receive buffers
	byte[] sendData;
	byte[] receivedData = new byte[534];
	InetAddress ipaddr;
	int client_port;
	Ex5(){
		super();
	}
	public void rdt_connect(String host , int client_port) throws IOException{
			this.client_port = client_port;
			// creating client socket
			socket = new DatagramSocket();
			// getting host address
			ipaddr = InetAddress.getByName(host);
				
	} 
	public void rdt_listen(int port) throws SocketException{
		socket = new DatagramSocket(port);
		//DatagramPacket packet = new DatagramPacket(buffer, buffer.length );
	}
	public void rdt_send(String inString) throws IOException{
		sendData = new byte[534];
		System.out.println("Enter something: ");
		System.out.println("data: "+inString+" "+sendData);
		// getting input
		sendData = inString.getBytes();
		// load the packet to the buffer
		send_pkt = new DatagramPacket(sendData,inString.length(), ipaddr,client_port );
		System.out.println("get pkt data: "+send_pkt.getData());
		//return send_packet;
	}
	public byte[] rdt_recv(byte[] rd) throws IOException{
		rd = new byte[534];
		rcv_pkt = new DatagramPacket(rd, rd.length );
		//socket.receive(received_packet);
		receivedData = rd;
		return receivedData;
	}
	public byte[] rdt_select() throws IOException{
		while(!receivedData.equals(null)){
			//return receivedData;
			this.rdt_recv(receivedData);
		}
		send_all();
		return null;
		
			
		//}
	}
	public void send_all() throws IOException{
		//byte[] send = new byte[534];
		//send_pkt = this.rdt_send();
		socket.send(send_pkt);
		//((PrintStream) socket).flush();
	}
	public void select() throws IOException, InterruptedException{
		this.recv_from();
	//	this.rdt_recv(receivedData);
		socket.wait();
		if(receivedData != null){
			this.statemachine_send(receivedData);
		}
		
	}
	public void recv_from() throws IOException{
		socket.receive(rcv_pkt);
		//statemachine_send(rcv_pkt);
	}
	public void statemachine_send(byte[] rcvData) throws IOException{
		statemachine_recv(rcvData);
	}
	public String statemachine_recv(byte[] rcvData) throws IOException{
		String data = new String(rcv_pkt.getData(),0,rcv_pkt.getLength());
		if(data != null){
			//return data;
			this.rdt_recv(data.getBytes());
		}
		else
			this.send_all();
		//System.out.println("Server: Data received: "+data);
		return "";
		
	}
	
	
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 28 2008
Added on Nov 28 2008
14 comments
473 views