[4613] in java-interest
Help needed with UDP-client & server
daemon@ATHENA.MIT.EDU (Tim Moons)
Tue Jan 9 14:08:39 1996
Date: Tue, 09 Jan 1996 17:17:28 +0000
From: Tim Moons <tim@innet.be>
To: java-interest@java.Eng.Sun.COM
This is a multi-part message in MIME format.
-----------------------------73382773518973
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hello all,
I'm programming for a long time Java now.
I try to write a client and a server. NOT in TCP, but UDP.
The SOURCE-codes are ATTACHED.
The client and server are compiling correcly, but there is an endless
loop in my server.
The prob :
I start my server, which constantly looks to port 7000. If the client is
started, he sends an UDP-Packet to port 7000. At the time this packet is
sent, the server starts in an endless loop. This means that there is a
connection between the client and the server, but something goes wrong,
can someone tell me what ?????
Tim Moons...............
Thanks to everybody who can help me !!!!
-----------------------------73382773518973
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="UDPSer.java"
// THIS IS THE SERVER !!!!!
// IT IS AN APPLICATION (STAND-ALONE PROGRAM)
// COMPILE IT AND RUN IT LIKE : 'java UDPServer'
import java.net.*;
import java.io.*;
class Server extends Thread {
DatagramSocket server;
byte ibuf[];
int iPort=7000;
public static String response, responseUpper;
public void run() {
DatagramPacket receive_Packet;
int ilength=256, i;
boolean loop=true;
while (loop) {
try {
try {
StringBuffer s=new StringBuffer();
receive_Packet = new DatagramPacket(ibuf, ilength);
server.receive(receive_Packet);
for (i=0 ; i<ilength ; i++) {
char c=(char)ibuf[i];
if (i<ilength-1) s.append(c);
}
response=s.toString();
responseUpper=response.toUpperCase();
}
catch (IOException e) {
System.out.println("an IO exception occurred: " + e.getMessage());
e.printStackTrace();
}
System.out.println("De server heeft ontvangen : " + response);
System.out.println("De server heeft dit gecodeerd als : " + responseUpper + " en naar de client gestuurd");
}
catch (Exception e) {
System.out.println("an IO exception occurred: " + e.getMessage());
e.printStackTrace();
loop=false;
return;
}
}
stop();
}
public void acceptRequests() {
try {
server=new DatagramSocket(iPort);
while (true) {
System.out.println("Accept");
run();
}
}
catch (Exception e) {
System.out.println("exception from Socket server " + iPort);
return;
}
}
}
public class UDPServer {
public static void main(String args[]) {
Server testserver;
testserver=new Server();
testserver.acceptRequests();
}
}
-----------------------------73382773518973
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="UDPCli.java"
// THIS IS THE CLIENT !!!!!
// IT IS AN APPLET (RUN IT IN THE APPLETVIEWER)
// COMPILE IT AND RUN IT LIKE : 'appletviewer ?????.html'
import java.io.*;
import java.lang.*;
import java.net.*;
import java.awt.*;
public class UDPClient extends java.applet.Applet {
int iport = 7000;
InetAddress iaddr;
public static String response;
DatagramSocket client;
public void connect() {
try {
client = new DatagramSocket(iport);
}
catch (Exception e) {
System.out.println("an IO exception occurred: " + e.getMessage());
e.printStackTrace();
}
}
public void sendString(String s) {
int ilength, j;
byte ibuf[];
DatagramPacket sent_Packet;
ilength = s.length();
ibuf = new byte[ilength+1];
for (j=0 ; j<ilength ; j++) {
ibuf[j] = (byte)s.charAt(j);
}
ibuf[ilength] = (byte)'\n';
try {
sent_Packet = new DatagramPacket(ibuf, ilength, iaddr.getByName("194.7.1.41"), iport);
client.send(sent_Packet);
}
catch (Exception e) {
System.out.println("an IO exception occurred: " + e.getMessage());
e.printStackTrace();
}
}
public void close_connection() {
System.out.println("The inputstream is closed !");
try {
sendString("gedaan");
client.close();
}
catch (Exception e) {
System.out.println("an IO exception occurred: " + e.getMessage());
e.printStackTrace();
}
}
public void init() {
resize(400,100);
UDPClient t;
t=new UDPClient();
t.connect();
t.sendString(new String("Hallo, deze tekst beweegt tussen client en server !!!!!"));
t.close_connection();
repaint();
}
public void paint (Graphics g) {
g.drawString("De client kreeg van de server terug", 10,30);
}
}
-----------------------------73382773518973--
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com