Solved

Sending a control string

Posted on 2004-05-01
16
320 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 97
hibernate example issues from command prompt 10 41
What's wrong with this code? 4 23
eclipse console opening separately 2 21
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 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