Hey,
I need some quick help with some code I am writing. I am send a packet string line from one computer to another as part of some course work. I need to use a Socket. I have used a DatagramSocket and it works fine but this does not.
I will display the code and my problem at the bottom.
Client
package socketstream; import java.io.*; import java.net.*; /** * * @author */ public class Main { public static void main(String[] args) throws InterruptedException { try { InetAddress DataIP = null; String temp = "100.100.0.5"; DataIP = InetAddress.getByName(temp); int DataPort = 4999; //Creates the Stream between server and client Socket mySkt = new Socket (DataIP,DataPort); //The stream to read data is created BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream())); //The stream to send data is created PrintStream myPS= new PrintStream(mySkt.getOutputStream()); myPS.println("Get up"); temp = myBR.readLine(); System.out.println(temp); System.out.print("sorted!!!"); } catch (UnknownHostException ex) { } catch(IOException ie){} } }
Server
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package serverstreamtest; /** * * @author */ public class Main { public static void main(String[] args){ int i =1; int ii = 4999; createStreamSocket cA = new createStreamSocket(4999);//creating the small array of users (10) // cA = new createStreamSocket(ii); // System.out.print(cA); // ii = ii -10; // cA[i+1] = new createStreamSocket(ii); // System.out.print(cA[i+1]); // ii = ii -10; // cA[i+2] = new createStreamSocket(ii); // System.out.print(cA[i+2]); } } /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package serverstreamtest; /** * * @author */ import java.io.*; import java.net.*; class createStreamSocket implements java.io.Serializable { public int userPort; BufferedReader SS_BR; //This is the bufferreader that will read in all the data sent accross //the two communications. This has been made public so it is accessable all over the class PrintStream SS_PS; createStreamSocket(int userPort) throws InterruptedException { try { this.userPort = userPort; // The user port number is recognised to be recieved on ServerSocket mySS = new ServerSocket(userPort);// the communication channel is opened Socket SS_accept = mySS.accept(); //This new buffer reader will get the streams being input to it from the client. This effectivly sets up its stream //of constant communication // String Ipaddress = InputStreamReader(mySS.accept().getInetAddress().toString().substring(1)); SS_BR = new BufferedReader(new InputStreamReader(SS_accept.getInputStream())); String temp = SS_BR.readLine(); System.out.print(temp); // try { // Thread.sleep(1000); // } catch (InterruptedException ex) { // // } //The stream to send data is created Thread.sleep(2111); SS_PS= new PrintStream(SS_accept.getOutputStream()); SS_PS.println("50"); clientPresent(); } catch (IOException ex) { System.out.print("Unable to setup ServerSocketStream on port "+ userPort);} } private void clientPresent() { } }
My problem is that the package is recieved and displayed by the server and the package "50" is sent back to the client (I think). However the client WILL NOT pick it up!! I do not have a clue why this is? Is it because I am using a real IP and sending between two actual computers?
The client Says build successful but wont display the System.out.print stuff!!
Also if I try to debug on the line System.out.print(temp); It just wont stop at that point the build ends!
Please help I need to do this project ASAP and this socket is the key to getting it done properly