Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Network simple issue

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Network simple issue

    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


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Network simple issue

    SOLVED:

    I wasn't using .close();

    This was mucking it up!!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java Network simple issue

    I am glad you've solved the problem. For future reference, please do not post the same question twice to the forums (your other post has been removed), and when you have solved the problem please go the Thread Tools -> Mark as Solved

Similar Threads

  1. Java Network performance analyser program help
    By Tark221 in forum Java Theory & Questions
    Replies: 4
    Last Post: March 22nd, 2011, 01:02 PM
  2. Java and db2 connection issue
    By sukhpal48806 in forum JDBC & Databases
    Replies: 2
    Last Post: March 1st, 2011, 12:07 PM
  3. Java uberNoob, requesting help with simple loop issue
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 09:13 PM
  4. java graphics2d issue
    By nana-j13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2010, 03:49 PM
  5. How to enumarete Network Nodes using Java
    By dilshadpaleri in forum Java Networking
    Replies: 0
    Last Post: September 7th, 2010, 06:45 AM