Solved

keeping multiple communication sockets/ports open using siemens TC45 moduke

Posted on 2003-12-03
3
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 48

Accepted Solution

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vibration sensors: piezoelectric to wireless 9 57
DNS with WiFi Network 5 91
How DHCP Works in Wired/Wireless network 21 234
Cisco ACS Adding Root and Intermediate Certs 2 64
Coaxial cable bending There are several factors that govern the selection of coaxial cable for your Machine to Machine (M2M) application: the location of cable runs, either indoor or outdoor, inside or outside an enclosure, maximum bending and the…
This article is a step by step guide on how to create a basic PTP link using Ubiquiti airOS devices. This guide can be used on the following Ubiquiti AirMAX devices. Nanostation, Bullets, AirBridge, Nanobeam, NanoBridge to name a few. Please review …
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…

734 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