Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

keeping multiple communication sockets/ports open using siemens TC45 moduke

Posted on 2003-12-03
3
Medium Priority
?
443 Views
Last Modified: 2012-08-14
Hi
 here is an explanation of the problem I am having.
The scenario can be duplicated as follows:

- I have a Main class that starts up a Thread that opens a server socket. The code for the listener thread is as follows:-

/*
 * ListenerThread.java
 *
 *  */

package com.applicationjunction.messenger;
import javax.microedition.io.*;
/**
 *
 * @author  Administrator
 */
public class ListenerThread extends Thread {
 
    private boolean running = false;
    private final Main main;
    //private WorkThread workThread;
 
    /** Creates a new instance of ListenerThread */
    public ListenerThread(Main main) {
        super();
        this.main = main;
    }
 
    public synchronized void start() {
        running = true;
        super.start();
    }
 
    public synchronized void kill() {
        running = false;
    }
 
    public void run() {
        try {
            StreamConnectionNotifier notifier = null;
            StreamConnection connection = null;
            java.io.InputStream is = null;
            java.io.OutputStream os = null;
         
            notifier = (StreamConnectionNotifier) Connector.open("serversocket://:9000",Connector.READ_WRITE,false);
            System.out.println("Waiting for connections on port 9000");
            while (running) {
                // wait for a incoming connection
                connection = notifier.acceptAndOpen();

                System.out.println("Connection received from " + connection.toString());
                os = connection.openOutputStream();
                is = connection.openInputStream();
                System.out.println("Connected to " + connection.toString());
                System.out.println("Sending \"OK\"...");
                // ready to read and write to the stream...
                os.write("ok".getBytes());
                os.flush();
                System.out.println("Message sent.");
                os.close();
                is.close();
                connection.close();
                connection = null;
            }
         
        }   catch (Exception err) {
            System.out.println("Something went wrong with the Listener Thread.");
            err.printStackTrace();
        }
    }
}

This causes the thread to listen for connections on port 9000. This works fine.

- The Main class then opens a socket connection to another module listening on port 9000. The code is as follows:-

        StreamConnection test1 = null;
        InputStream test1in = null;
        OutputStream test1out = null;
        try {
            test1 = (StreamConnection) Connector.open("socket://192.168.0.4:9000",Connector.READ_WRITE,false);
            test1in = test1.openInputStream();
            test1out = test1.openOutputStream();
           
            System.out.println((char) test1in.read());
            System.out.println((char) test1in.read());
           
            test1in.close();
            test1out.close();
            test1.close();
            test1 = null;
        } catch (IOException ioe) {
            System.err.println("Could not receive from other box");
        }

At this point the box is listening for incomming connections and trying to make an outgoing connection to a separate box.
After the socket connection is made, the server socket connection dies with the following stackTrace printout.

java.io.IOException
        at com.sun.cldc.io.j2me.serversocket.Protocol.acceptAndOpen(+4)
        at com.applicationjunction.messenger.ListenerThread.run(+37)

That is all I can give you to replicate the problem. All this is happening over a GPRS connection using the Siemens TC45 device.
I hope you can help me out.

0
Comment
Question by:gregkl
1 Comment
 
LVL 48

Accepted Solution

by:
Mikal613 earned 1500 total points
ID: 9867054
First Make the Socket Connect and Then and then Listen for incoming connections
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Need WiFi? Often, there are perfectly good networks that don't have WiFi capability - and there's a need to add it.  - Perhaps you have an Ethernet port into a network but no WiFi nearby. - Perhaps you have a powerline extender and no WiFi at the…
The Summer 2017 Scholarship Winners have been announced!
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
Suggested Courses

773 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