Solved

Sending a control string

Posted on 2004-05-01
16
318 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
  • 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
 
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:mayankeagle
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now