Hi, I need to set up dynamic IP address in my applet client program for socket purpose. Thanks Angela
maateen ashraf
Ranch Hand
Joined: Jan 08, 2001
Posts: 122
posted
0
just try to use myapplet.getCodeBase().toString() method to get the IP of the perticuler host.... & then make socket connection.... tell if still problem existes....
hi anand & marteen, Thanks for your reply. But doesn't work. Here is my code: import java.net.*; import java.io.*; import java.util.*; import java.net.*; import java.lang.String; import java.net.InetAddress;
public class K_Socket extends Thread { private Socket localSocket; private Socket sclient; private BufferedReader in; protected K_API sapi; protected K_SocketThread sthread; protected K_Main main; public Vector queue; String ipAddr; StringBuffer buf; int port,tmp,maxbuf,read; int bytesend = 0; char [] buffer; private Socket clientSocket; // defalult constructor public K_Socket() { try { InetAddress Address=InetAddress.getHostAddress(); String ipAddr = Address.toString(); } catch(UnknownHostException u) { } //"10.3.158.172"; port = 9999; bytesend = 0; maxbuf = 2000; char [] buffer = new char[maxbuf]; tmp = 0; queue = new Vector(); } //open the socket to remote host public void open() { try { localSocket = new Socket(Address,port); System.out.println("The Host IP address is:" + localSocket.getInetAddress()); } catch(UnknownHostException uhe) { System.out.println("The Host is Unknown"); } catch(IOException ioe) { System.out.println("I/O Error"); } } //To send the data to the server public void send() { try { clientSocket = localSocket; PrintWriter out; BufferedReader inner; out = new PrintWriter(clientSocket.getOutputStream()); String s = "Hello this Konica Client is connected"; buffer = s.toCharArray(); tmp = s.length(); out.write(buffer,0,tmp); out.flush(); bytesend = bytesend + tmp; System.out.println("Bytes send:" + bytesend); System.out.println("The Message sent to server is" + s); inner = new BufferedReader(new InputStreamReader(localSocket.getInputStream())); read = inner.read(buffer,0,tmp); StringBuffer r = new StringBuffer(); //r.append(buffer,0,tmp); r.insert(0,buffer); String mesg = r.toString(); System.out.println("The Received message is:" + r); } catch(IOException ie) { System.out.println("Error Sending Data"); } } //To receive the data from the server public void receive() { //declaration int tmp1, maxbuf = 2000; char[] buffer = new char[maxbuf]; BufferedWriter fwrt = null; try { in = new BufferedReader(new InputStreamReader(localSocket.getInputStream())); // loop to read data from server while((tmp1 = in.read(buffer,0,2000))>=0) { System.out.println("current bytereceive:"+tmp1); //procMsg(buffer,0,tmp1); if(tmp1<0) { System.out.println("Can not have a negative string"); return; } buf = new StringBuffer(); buf.append(buffer,0,tmp1); System.out.println("I Have read the Buffer mesg from Server:" + buf); String msg = buf.toString(); putQ(msg); } System.out.println("Total Bytes Received:" + tmp1); fwrt.close(); } catch(IOException e) { System.out.println("Reading Error"); } } public void putQ(String vec) { while(queue.isEmpty()) { queue.addElement(vec); System.out.println("The Message from Queue is:" + vec); queue.removeElement(vec); receive(); } System.out.println("Waiting for data to come in"); } public void run() { receive(); } } //End of the class Plz let me know, thanks angela
Originally posted by anand rk: Hey just try this code and tell me if it works