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 6 of 6

Thread: Problems in reading/writing from/to a socket

  1. #1
    Junior Member Fabgio's Avatar
    Join Date
    Feb 2013
    Location
    Padua, Italy
    Posts
    8
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problems in reading/writing from/to a socket

    I have been trying to send a command string to my domotics web server which is connected to a bus and then to my LAN. The command that has to be sent to the web server through the bus is a string like this: *1*1*52## where 52 is my room's power point address, and the second "1" of the command is an on switch status (0 stands for off). So the light of my room is get switched on. My webserver IP adress is: 192.168.1.36. My goal is writing the string to the server to execute commands as well as reading from that I wrote some Java Code but the program neighter reads nor writes from/to the web server. I am very frustrated about that.
    Here is the code:
    import java.io.*;
    import java.net.*;
     
    public class ClientExample extends Thread {
        ClientExample() {
            Thread t = new Thread();
            t.start();
        }
        public static void main(String[] args) throws InterruptedException {
            String str;
            String ip = null;
            String Command = null;
            int port;
            ip = "192.168.1.36";
            port = 80;
            try {
                Socket s = new Socket(ip,port);//creating  socket object
                System.out.println("port:  " + s.getPort());
                OutputStream outputstream = s.getOutputStream();
                InputStream inputstream = s.getInputStream();
                BufferedReader in = new BufferedReader(new InputStreamReader(inputstream));
                PrintWriter out = new PrintWriter(outputstream, true);
                System.out.println("Connection status:  " + s.isConnected());
                System.out.println("Reading from socket....");
                do{
                str=in.readLine();
                if (str!=null)
                    System.out.print("str  "+str);
                } while (str!=null);
    //            Thread.sleep(5000);
                Command = "*1*0*52##";
    //            Thread.sleep(5000);
                System.out.println("writing to socket....");
                out.write(Command);
                System.out.println(("Command:    " + Command));
                System.out.println("check for errors :  " + out.checkError());
                System.out.println(out);
                in.close();
                out.close();
                s.close();
            } catch (IOException e) {
                System.out.println("an error occured");
            }
        }
    }
    Any help will be appreciated!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problems in reading/writing from/to a socket

    ClientExample() {
            Thread t = new Thread();
            t.start();
        }
    What does this thread do?

  3. The Following User Says Thank You to jps For This Useful Post:

    Fabgio (June 17th, 2013)

  4. #3
    Junior Member Fabgio's Avatar
    Join Date
    Feb 2013
    Location
    Padua, Italy
    Posts
    8
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problems in reading/writing from/to a socket

    Basically I set up a thread just to give a sleep() to the main() with the aim to get any result. Still I desided to comment it out afterwards. So you could not take it into consideration.

  5. #4
    Junior Member Fabgio's Avatar
    Join Date
    Feb 2013
    Location
    Padua, Italy
    Posts
    8
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problems in reading/writing from/to a socket

    By the way, hanging around the Internet I found out a dedicated Java class performing what I need. Anyway thank you so much for your support!

  6. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems in reading/writing from/to a socket

    how can i read the electrical sensor data through analog to digital converter in my personal computer through java programming???

  7. #6
    Junior Member Fabgio's Avatar
    Join Date
    Feb 2013
    Location
    Padua, Italy
    Posts
    8
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problems in reading/writing from/to a socket

    For information about Bticino my home system go to the following link:
    bticino - Local and remote control

Similar Threads

  1. Reading and Writing Word Documents
    By Esanders in forum Java Theory & Questions
    Replies: 1
    Last Post: April 26th, 2013, 08:48 PM
  2. Boolean writing and reading
    By wuppy29 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 13th, 2012, 02:21 PM
  3. reading unformatted data from socket
    By amjhanwar in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 7th, 2012, 02:03 PM
  4. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM
  5. Problem reading from server socket
    By glauber in forum Java Networking
    Replies: 0
    Last Post: February 15th, 2011, 11:54 AM