Solved

Sending a control string

Posted on 2004-05-01
16
319 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: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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

914 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

14 Experts available now in Live!

Get 1:1 Help Now