Solved

Sending a control string

Posted on 2004-05-01
16
322 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 65
hibernate insert example 13 34
Java string replace 11 54
Java Eclipse Loop 3 20
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 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