×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Unhappy SocketConnection Work on 6600&Emulators BUT NOT IN 6680

    I'm last year university student and as a team we are trying to develop chat application for s60 that its server runs on desktop.

    I develop really basic server and test with desktop clients, and it run nice.
    Then I develop for s60. When I compilded it midp2.0&cldc1.0 for 6600 it can send and receive messages, however when I compiled it midp2.0&cldc1.1 for 6680 it only sends.

    When I looked to connection manager I saw incoming data is occured, however in thread it stopped on readBytes = userIS.read(data) and can't continue so that it can't write the data to TextField.

    PLEASE HELP ME, BECAUSE I HAVE NOT SO MUCH TIME TO FINISH.
    Thank you

    Client Codes:


    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.microedition.io.Connector;
    import javax.microedition.io.SocketConnection;




    import javax.microedition.lcdui.Display;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;

    public class BookClientMIDlet extends MIDlet {
    private static BookClientMIDlet instance;
    private static ChatForm chatForm;

    public BookClientMIDlet() {
    super();
    instance = this;
    }

    protected void startApp() throws MIDletStateChangeException {
    chatForm = new ChatForm();
    Display.getDisplay(this).setCurrent(chatForm);
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    public static void quitApp() {
    try {
    instance.destroyApp(true);
    }
    catch (MIDletStateChangeException e) {
    }
    instance.notifyDestroyed();
    instance = null;
    }

    }


    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.microedition.io.Connector;
    import javax.microedition.io.SocketConnection;




    public class ChatClient {
    private String nickName;
    private ListenThread serverListener;
    private SocketConnection client;
    private InputStream input;
    private OutputStream out;
    private ReceiverInterface userApp;

    public ChatClient(ReceiverInterface app, String server, String port, String nickname) {
    userApp = app;
    nickName = nickname;
    try {
    client = (SocketConnection)Connector.open("socket://" + server + ":" + Integer.parseInt(port));

    out = client.openOutputStream();
    input = client.openInputStream();
    serverListener = new ListenThread(input);
    serverListener.start();
    sendToServer(nickName + " connected.\n");
    }
    catch(IOException ioex) {
    ioex.printStackTrace();
    System.exit(1);
    }
    }

    public synchronized void sendToServer(String message) {
    try {
    String withHeader = new String(nickName + "> " + message);
    out.write(withHeader.getBytes());
    out.flush();
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }

    class ListenThread extends Thread {
    private InputStream userIS;
    private byte[] data = new byte[1000];
    private int readBytes;

    public ListenThread(InputStream is) {
    userIS = is;
    }

    public void run() {
    while(true) {
    try {
    userApp.received(userIS.toString());
    readBytes = userIS.read(data);
    userApp.received(new String(data, 0, readBytes));
    }
    catch (Exception e) {
    System.out.println(e);
    break;
    }
    }
    }
    }
    }


    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.TextField;

    public class ChatForm extends Form implements CommandListener, ReceiverInterface{
    private TextField incoming = new TextField("Incoming", "", 500, TextField.UNEDITABLE);
    private TextField outgoing = new TextField("Outgoing", "", 500, TextField.ANY);
    private Command sendCommand = new Command("Send", Command.ITEM,1);

    private ChatClient cc;

    public ChatForm() {
    super("Chat");
    append(incoming);
    append(outgoing);
    addCommand(sendCommand);
    setCommandListener(this);

    cc = new ChatClient(this, "<The IP of Server>", "<The port>, "Mobile");
    }

    public void commandAction(Command command, Displayable displayable) {
    if(command.getCommandType() == Command.EXIT) {
    BookClientMIDlet.quitApp();
    }
    else if(command.getLabel().equals("Send")) {
    cc.sendToServer(new String(outgoing.getString() + "\n"));
    outgoing.setString("");
    }
    }

    public void received(String message) {
    incoming.setString(incoming.getString() + message + "\n");

    }
    }



    public interface ReceiverInterface {
    public void received(String message);
    }




    AND THE SERVER:



    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.StringTokenizer;
    import java.util.Vector;


    public class ChatServer {
    private Vector users = new Vector();
    private ServerSocket serverSocket;
    private Socket newClient;
    private InputStream is;
    private OutputStream os;

    public ChatServer() {
    try {
    serverSocket = new ServerSocket(6644);
    System.out.println("MCS Chat Server is listening on port 6644");
    } catch (IOException e) {
    System.out.println("MCSErr-CS-101: Can't start chat server on port.");
    e.printStackTrace();
    System.exit(1);
    }

    while(true) {
    try {
    newClient = serverSocket.accept();
    System.out.print("New Client from " + newClient.getRemoteSocketAddress() + "\n");
    is = newClient.getInputStream();
    os = newClient.getOutputStream();
    users.add(os);
    ClientThread clientThread = new ClientThread(is, os);
    clientThread.start();
    }
    catch(IOException ioe) {
    System.out.println("MCSErr-CS-102: New client thread problem.");
    ioe.printStackTrace();
    }
    }
    }


    public synchronized void sendMessage (String messageWithProtocol) {
    for(int x = 0; x < users.size(); x++)
    {
    OutputStream os = (OutputStream) users.elementAt(x);
    try {
    os.write(messageWithProtocol.getBytes());
    }
    catch(IOException e) {
    System.out.println("MCSErr-CS-105: Can't send message to client");
    e.printStackTrace();
    }
    }
    }

    class ClientThread extends Thread {
    InputStream userIS;
    OutputStream userOS;
    String nickname;
    private byte[] data = new byte[1000];
    private int readBytes;


    public ClientThread(InputStream is, OutputStream os) {
    userIS = is;
    String firstLine = null;

    try {
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    firstLine = br.readLine();
    }
    catch(IOException ioe) {
    System.out.println("MCSErr-CS-104: User thread problem.");
    ioe.printStackTrace();
    }

    StringTokenizer st = new StringTokenizer(firstLine);
    st.nextToken();
    nickname = st.nextToken();
    System.out.println(" nickname " + nickname);
    sendMessage(firstLine + "\n");
    userOS = os;
    }

    public void run() {

    while(true) {
    try {
    readBytes = userIS.read(data);
    if (readBytes != -1) {
    sendMessage(new String(data, 0, readBytes));
    }
    }
    catch(IOException ioe) {
    users.remove(userOS);

    String info1 = "server> " + nickname + " ayrildi. ";
    String info2 = "# of connected clients: " + users.size();
    sendMessage(info1 + info2 +"\n");
    break;
    }
    }
    }
    }



    public static void main(String[] args) {
    ChatServer chatServer = new ChatServer();
    }
    }

  2. #2
    Nokia Developer Expert
    Join Date
    Jun 2005
    Posts
    923

    Re: SocketConnection Work on 6600&Emulators BUT NOT IN 6680

    It seems that InputStream methods have some issues in 6680, so you can try something slightly different to see if the connection works:

    Code:
    public void run() {
      while(true) {
        try {
          userApp.received(userIS.toString());
          StringBuffer sb = new StringBuffer();
          int c;
          while((c = is.read())!=-1) {
             sb.append((char)c);
          }
          userApp.received(sb.toString());
        }
        catch (Exception e) {
          System.out.println(e); 
          break;
        }
      }
    }
    Daniel

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Re: SocketConnection Work on 6600&Emulators BUT NOT IN 6680

    Thank you it works well, but this time I have problems with UTF special characters (language specific, especially Turkish characters that are included in UTF-8 and UTF-16)

    Can you offer me a solution for this too?

    Thank you very much again..

  4. #4
    Nokia Developer Expert
    Join Date
    Jun 2005
    Posts
    923

    Re: SocketConnection Work on 6600&Emulators BUT NOT IN 6680

    Hi,

    If you need to read UTF-8 characters, you should use the InputStreamReader class, as it has a constructor that takes: 1) The InputStream you wish to read from 2) the encoding you want, UTF-8 in that case.

    After that you can use the Reader to read unicode characters from that stream.

    Hope it helps.

    Daniel

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Re: SocketConnection Work on 6600&Emulators BUT NOT IN 6680

    It didn't work again. But, I found a solution that work on all of them. I read it to byte[] in a loop by InputStream's read method without giving any parameters. And then I convert byte[] to string by using String class.
    like that; new String(byte[], 0, readBytes, "UTF-8");

    thank your helps again...

Similar Threads

  1. Server Socket Problem on Nokia 6680: acceptAndOpen does not work
    By opistara in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2009-08-06, 11:20
  2. Nokia 6600 Network (Http and Socket) connections do not work!
    By jimmichr in forum Mobile Java Networking & Messaging & Security
    Replies: 11
    Last Post: 2006-07-14, 07:48
  3. Connecting Nokia 6680 to SonyEricsson k600
    By nicky64 in forum Mobile Java Networking & Messaging & Security
    Replies: 5
    Last Post: 2006-03-24, 08:53
  4. 6680 maxing out memory too fast
    By earamsey in forum Mobile Java General
    Replies: 0
    Last Post: 2005-10-21, 22:15
  5. Replies: 0
    Last Post: 2004-10-18, 18:06

Posting Permissions

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