This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes socket udp programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "socket udp programming" Watch "socket udp programming" New topic
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();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: socket udp programming
 
Similar Threads
problem in name resolution
Problem in UDP client/server application
Problem in UDP client/server application....
Audio in Java
how to send sound buffer in UDP