Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Sep 2007

    Problem with Java Client Socket with C# Server

    I've been stuck on this problem for several days already. I hope there's someone who knows C# as well and can help me. From the code below, the server works when i first try to read some bytes from the client and then perform a reply send. But when i added some more code to try to receive bytes from the client again, the whole server application goes haywire and does not even send the first reply.

    The problem probably lies in the server code but maybe its something i've done wrongly in the J2ME client code. It may also be because there's output and input stream in J2ME while there is only a network stream in C#. Thanks so much for your help!

    Server Code in C#:

    TcpListener myList = new TcpListener(ipAd,13000);
    Socket s = myList.AcceptSocket();
    NetworkStream ns = new NetworkStream(s);

    byte[] ba = new byte[40];
    ns.Read(ba, 0, ba.Length);
    //..some operations with ba

    byte[] myWriteBuffer = null;
    myWriteBuffer = Encoding.Unicode.GetBytes("1");


    byte[] bb = new byte[40]
    //.. some operations with bb


    Client Code in J2ME:

    String name = "socket://" + IPAddress + ":" + 13000;
    sc = (StreamConnection)Connector.open(name,Connector.READ_WRITE);

    os = sc.openOutputStream();

    StringBuffer sb = new StringBuffer();
    byte[] data = textbox1.getBytes();

    is = sc.openInputStream();
    byte[] incoming = new byte[5];

    while (is.read(incoming)!= -1) {
    //do something to the byte

    if(is !=null){ // This statement may be the problem
    is.close(); // because when i remove the if statement
    is = null; // and just close the stream, an exception occurs
    } // but i do not understand why

    os = sc.openOutputStream();
    byte[] data2 = textbox2.getBytes();


    Thank you for your help!

  2. #2
    Regular Contributor
    Join Date
    Aug 2006

    Re: Problem with Java Client Socket with C# Server

    Just IMHO.

    In your code you're using the StreamConnection, while you should use the SocketConnection. You also probably shouldn't call the close() method of the output and input streams, but the flush() methods instead.

    I'm not a specialist in C#, but I think the SDK provided should have some good examples on the topic.

    Also take a look at this End-to-End Sockets API Example v1.1: http://sw.nokia.com/id/9acddf62-669c...le_v1_1_en.zip

  3. #3
    Regular Contributor
    Join Date
    Dec 2006

    Re: Problem with Java Client Socket with C# Server

    Does application must use flush/close after wrinting to the socket's openOutputStream();?
    If yes, Does it means that the application need to open the sockets everytime when message need to be sent?
    How can it be overcome? How can I leave the openOutputStream() open and still writting to?

  4. #4
    Regular Contributor
    Join Date
    Aug 2006

    Re: Problem with Java Client Socket with C# Server

    The connection.openOutputStream() method returns an OutputStream object:

    OutputStream output = connection.openOutputStream();

    Once you've got the output, you call it's write() and flush() methods for putting data into it and send:

    output.write(byte[] data);

    There are no needs to call openOutputStream() anymore, since the same output may be used for sending different messages many times.

    output.write(byte[] yet_another_data);

    The flush() method clears an internal output buffer of the output stream, and pushes written data to its network destination.

    Calling the close() method of output will close it, and after that it couldn't be used for data sending anymore.
    Last edited by axs; 2007-09-25 at 07:01.

Similar Threads

  1. Bluetooth client problem on games!
    By wmchan78 in forum Mobile Java Networking & Messaging & Security
    Replies: 6
    Last Post: 2006-03-22, 06:15
  2. Odd Client Server Problem, Not Finding EXE
    By ViruDoshi in forum Symbian
    Replies: 0
    Last Post: 2006-03-13, 02:38
  3. Replies: 0
    Last Post: 2005-09-20, 03:58
  4. [Bluetooth] Client and server at the same time?
    By titan_ordalie@yahoo.fr in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2005-08-26, 20:03
  5. Socket problem
    By defragger in forum Symbian
    Replies: 0
    Last Post: 2005-08-25, 08:16

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts