| Author |
socket udp programming
|
Priya Kubher
Greenhorn
Joined: Jan 07, 2003
Posts: 10
|
|
Hi iam trying to set up a client server in udp. So far my program sends messages if both client and server are run on the same machine. How do i get it to work if they run on different machines. Do advise. Sorry to dump this many lines of code but i think iam missing something important here. Thank you for all your help my client code: import java.net.*; class ucVoipClient { public static int serverPort = 666; public static int clientPort =999; public static int buffer_size = 1024; public static DatagramSocket ds; public static byte buffer[] = new byte[buffer_size]; public static void TheClient( ) throws Exception { while(true) { DatagramPacket p = new DatagramPacket(buffer, buffer.length); ds.receive(p); System.out.println(new String(p.getData(),0,p.getLength( ))); } } public static void main (String args[]) throws Exception { ds = new DatagramSocket(clientPort); TheClient(); } } server code is: import java.net.*; class ucVoipServer { public static int serverPort = 666; public static int clientPort =999; public static int buffer_size = 1024; public static DatagramSocket ds; public static byte buffer[] = new byte[buffer_size]; public static void TheServer() throws Exception { int pos =0; while(true) { int i = System.in.read(); switch(i) { case -1: System.out.println( "Server Exits"); return; case '\t': break; case '\n': ds.send(new DatagramPacket(buffer,pos,InetAddress.getLocalHost(),clientPort)); pos=0; break; default: buffer[pos++] =(byte) i; } } } public static void main (String args[]) throws Exception { ds = new DatagramSocket(serverPort); TheServer(); } }
|
 |
 |
|
|
subject: socket udp programming
|
|
|