Solved

Sending a control string

Posted on 2004-05-01
16
323 Views
Last Modified: 2010-03-31
I am writing a client side application that must send a control command to a video projector via LAN. The string starts with a null character, followed by "1234,Operator,3:46:07 PM,01/05/2004,5,543210,DPL-123,lpsu,power,write,1,50" and terminated with a carriage return. The address of the projector is 192.168.200.13 at port 7000. I am used to writing in VB so I am not clear if there is a socket object like Winsock or what the appropriate way is to send the string. Thanks.
0
Comment
Question by:keithfrey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +2
16 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10969440
Socket s = new Socket("192.168.200.13", 7000);
PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream());
out.println("1234,Operator,3:46:07 PM,01/05/2004,5,543210,DPL-123,lpsu,power,write,1,50");

0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970143
object, i got a doubt , why use this
>PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream());

why not
PrintWriter out = new PrintWriter(s.getOutputStream());
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970144
object, i got a doubt , why use this
>PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream());

why not
PrintWriter out = new PrintWriter(s.getOutputStream());
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:gnoon
ID: 10970723
Socket socket = new Socket("192.168.200.13", 7000);
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.write(0x00); // a nul character
ps.flush();
ps.print("1234,Operator,3:46:07 PM,01/05/2004,5,543210,DPL-123,lpsu,power,write,1,50\r");
ps.flush();
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10971279
0
 

Author Comment

by:keithfrey
ID: 11199811
Here is the full program...

/*

program to send string to projector

*/

class SendString  {

public static void main(String args[])

{


Socket socket = new Socket("192.168.200.13", 7000);
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.write(0x00); // a nul character
ps.flush();
ps.print("1234,Operator,3:46:07 PM,01/05/2004,5,543210,DPL-123,lpsu,power,write,1,50\r");
ps.flush();


}

}

I got errors that javac could not resolve the class Socket, or PrintStream. What do I need to import and what else do I need to fix?
0
 
LVL 92

Expert Comment

by:objects
ID: 11199827
add the following to top of your class

import java.net.*;
import java.io.*;
0
 

Author Comment

by:keithfrey
ID: 11199841
Thanks,

Now I am down to 2 errors:

cannot resolve symbol
symbol: constructor Socket (java.lan.String,int)
location class Socket
Socket socket = new Socket("192.168.200.13", 7000);

cannot resolve symobl
symbol " method getOutputStream ()
location class Socket
PrintStream ps = new PrintStream(Socket.getOutputStream());

program is as follows...

/*

program to send string to projector
name of file is Socket.java

*/
import java.net.*;
import java.io.*;

class Socket  {

public static void main(String args[])

{


Socket socket = new Socket("192.168.200.13", 7000);
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.write(0x00); // a nul character
ps.flush();
ps.print("1234,Operator,3:46:07 PM,01/05/2004,5,543210,DPL-123,lpsu,power,write,1,50\r");
ps.flush();


}

}



0
 
LVL 92

Expert Comment

by:objects
ID: 11199855
Change the name of your class to something other than Socket.
0
 

Author Comment

by:keithfrey
ID: 11199868
Right,

I just did that, changed the name of the class to SendString, renamed the file SendString.java, and now have 4 errors. All errors are located in the class Socket.
0
 
LVL 92

Expert Comment

by:objects
ID: 11199893
/*

program to send string to projector
name of file is Socket.java

*/
import java.net.*;
import java.io.*;

class SendString  {

public static void main(String args[])
{
      try
      {
            Socket socket = new Socket("192.168.200.13", 7000);
            PrintStream ps = new PrintStream(socket.getOutputStream());
            ps.write(0x00); // a nul character
            ps.flush();
            ps.print("1234,Operator,3:46:07 PM,01/05/2004,5,543210,DPL-123,lpsu,power,write,1,50\r");
            ps.flush();
      }
      catch (Exception ex)
      {
            ex.printStackTrace();
      }

}

}
0
 

Author Comment

by:keithfrey
ID: 11199908
Ugh! 2 errors are below...

C:\Java Practice>javac Socket.java
.\Socket.java:10: duplicate class: SendString
class SendString {
^
Socket.java:16: cannot access Socket
bad class file: .\Socket.java
file does not contain class Socket
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
Socket socket = new Socket("192.168.200.13", 7000);
^
2 errors
0
 
LVL 92

Expert Comment

by:objects
ID: 11199924
delete Socket.java
0
 

Author Comment

by:keithfrey
ID: 11199933
Okay, I should have thought of that myself, sorry.

It looks like I am not trapping an error properly. I am not currently connected to the host, so I get the following errors...


C:\Java Practice>javac SendString.java
SendString.java:16: unreported exception java.net.UnknownHostException; must be
caught or declared to be thrown
Socket socket = new Socket("192.168.200.13", 7000);
                ^
SendString.java:17: unreported exception java.io.IOException; must be caught or
declared to be thrown
PrintStream ps = new PrintStream(socket.getOutputStream());
                                                       ^
2 errors

C:\Java Practice>

Ready to accept your answer! Thanks.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 11199943
see the code I posted above, it fixes those errors.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
type mismatch (Object[] to double[] 4 41
jboss wildfly 10.1 10 438
Adding multiple JVM environments to RedHat 6 7 70
jsp error 6 49
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question