Socket C# in Java umsetzen

napier

Neues Mitglied
Hallo

Kann mir jemand bitte diesen c# code in Java umzusetzten. Ich schaffe es einfach nicht ???:L

Danke und Gruss

Java:
public static void SendCommand(String command)
{
    try
    {
        StreamWriter clientStreamWriter = null;

        // hardcoded IP Address for now.....
        String onkyoIP = "192.168.1.69";

        int length = command.Length;
        length++;
        int total = length + 16;
        char code = (char)length;

        // build up packet header and rest of command - followed by <CR> (chr(13))
        //
        String line = "ISCP\\x00\\x00\\x00\\x10\\x00\\x00\\x00" + code + "\\x01\\x00\\x00\\x00" + command + "\\x0D";

        // create a TCP Client - hardcoded port too ....
        TcpClient tcpClient = new TcpClient(onkyoIP, 60128);

        //get a network stream from server
        NetworkStream clientSockStream = tcpClient.GetStream();

        // create new writer stream to send the stuff
        clientStreamWriter = new StreamWriter(clientSockStream);

        // send command to receiver
        //
        clientStreamWriter.WriteLine(line);
        clientStreamWriter.Flush();
        clientStreamWriter.Close();
        // close the socket
        tcpClient.Close();
    }
    catch (Exception e)
    {
        // should do something better here... but meh.. it's just for me
        Console.WriteLine(e.StackTrace);
    }
}
 
Ok Danke euch für die Hinweise. Funktioniert jetzt.

Java:
import java.io.*;
import java.net.*;


public class SimpleSocketClient {
    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        String host = "192.168.1.16";
        int port = 60128;
        String command = "!1PWR00";
        
        int length = command.length();
        length++;
 //       int total = length + 16;
        char code = (char)length;

        try {
            echoSocket = new Socket(host, port);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: "+ host +".");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to: "+ host +".");
            System.exit(1);
        }
        
        BufferedOutputStream bos = new BufferedOutputStream(echoSocket.getOutputStream());

            /** Instantiate an OutputStreamWriter object with the optional character
             * encoding.
             */
            OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
            
            String process = "ISCP" + (char) 0 + (char) 0 + (char) 0 + (char) 16 + (char) 0 + (char) 0 + (char) 0 + code + (char) 1 + (char) 0 + (char) 0+ (char) 0+ (char) 0+ command + (char) 13;

            /** Write across the socket connection and flush the buffer */
            osw.write(process);
            osw.flush();

	out.close();
	in.close();
	echoSocket.close();
    }
}
 

Zurück
Oben