Multiple threads but they are using the same thread ID

I'm a newbie in Java programming. I have the following codes, please take a look and tell me what wrong with my codes: server side written in Java, client side written in C. Please help me out, thanks for your time.
Here is my problem: why my server program assigns the same thread ID for both threads???
.Server side: start server program
.Client side: set auto startup in /etc/rc.local file in a different machine, so whenever this machine boots up, the client program will start automatically.
==> here is the result with 2 clients, why they always come up the same thread ID ????????
Waiting for client ...
Server thread 1024 running
Waiting for client ...
Server thread 1024 running
But if I do like this, they all work fine:
.Server side: start server program
.Client side: telnet or ssh to each machine, start the client program
==> here is the result:
Waiting for client ...
Server thread 1024 running
Waiting for client ...
Server thread 1025 running
server.java file:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.regex.Pattern;
import java.util.Date;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class Server extends Frame implements Runnable
private ServerThread clients[] = new ServerThread[50];
private ServerSocket server = null;
private Thread thread = null;
private int clientCount = 0;
//some variables over here
public Server(int port)
//GUI stuffs here
//network stuff
try
System.out.println("Binding to port " + port + ", please wait ...");
server = new ServerSocket(port);
System.out.println("Server started: " + server);
start();
catch(IOException ioe)
System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
public boolean action(Event e, Object arg)
//do something
return true;
public synchronized void handle(int ID, String input)
//do something
public synchronized void remove(int ID)
int pos = findClient(ID);
if (pos >= 0)
//remove a client
ServerThread toTerminate = clients[pos];
System.out.println("Removing client thread " + ID + " at " + pos);
if (pos < clientCount-1)
for (int i = pos+1; i < clientCount; i++)
clients[i-1] = clients;
clientCount--;
try
{  toTerminate.close(); }
catch(IOException ioe)
{  System.out.println("Error closing thread: " + ioe); }
toTerminate.stop();
private void addThread(Socket socket)
if (clientCount < clients.length)
clients[clientCount] = new ServerThread(this, socket);
try
clients[clientCount].open();
clients[clientCount].start();
clientCount++;
catch(IOException ioe)
System.out.println("Error opening thread: " + ioe);
else
System.out.println("Client refused: maximum " + clients.length + " reached.");
public void run()
while (thread != null)
try
{       System.out.println("Waiting for a client ...");
addThread(server.accept());
catch(IOException ioe)
System.out.println("Server accept error: " + ioe); stop();
public void start()
if(thread == null)
thread = new Thread(this);
thread.start();
public void stop()
if(thread != null)
thread.stop();
thread = null;
private int findClient(int ID)
for (int i = 0; i < clientCount; i++)
if (clients[i].getID() == ID)
return i;
return -1;
public static void main(String args[])
Frame server = new Server(1500);
server.setSize(650,400);
server.setLocation(100,100);
server.setVisible(true);
ServerThread.java file
import java.net.*;
import java.io.*;
import java.lang.*;
public class ServerThread extends Thread
private Server server = null;
private Socket socket = null;
private int ID = -1;
InputStreamReader objInStreamReader = null;
BufferedReader objInBuffer = null;
PrintWriter objOutStream = null;
public ServerThread(Server server, Socket socket)
super();
server = _server;
socket = _socket;
ID = socket.getPort();
public void send(String msg)
objOutStream.write(msg);
objOutStream.flush();
public int getID()
return ID;
public void run()
System.out.println("Server thread " + ID + " running");
while(true)
try{
server.handle(ID,objInBuffer.readLine());
catch(IOException ioe)
System.out.println(ID + "Error reading: " + ioe.getMessage());
//remove a thread ID
server.remove(ID);
stop();
public void open() throws IOException
//---Set up streams---
objInStreamReader = new InputStreamReader(socket.getInputStream());
objInBuffer = new BufferedReader(objInStreamReader);
objOutStream = new PrintWriter(socket.getOutputStream(), true);
public void close() throws IOException
if(socket != null) socket.close();
if(objInStreamReader != null) objInStreamReader.close();
if(objOutStream !=null) objOutStream.close();
if(objInBuffer !=null) objInBuffer.close();
And client.c file
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close */
#include <time.h>
#define SERVER_PORT 1500
#define MAX_MSG 100
//global variables
long lines = 0;
int sd = 0;
char command[100];
time_t t1,t2;
double timetest = 0.00;
int main (int argc, char *argv[])
int rc, i = 0, j = 0;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
char buf[100];
FILE *fp;
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("unknown host '%s'\n",argv[1]);
exit(1);
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(SERVER_PORT);
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
exit(1);
/* bind any port number */
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
if(rc<0) {
printf("%s: cannot bind port TCP %u\n",argv[1],SERVER_PORT);
perror("error ");
exit(1);
/* connect to server */
rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc<0) {
perror("cannot connect ");
exit(1);
//send register message
rc = send(sd, "register\n", strlen("register\n"), 0);
//if can't send
if(rc < 0)
close(sd);
exit(1);
//wait here until get the flag from server
while(1)
buf[0] = '\0';
rc = recv(sd,buf,MAX_MSG-1,0);
if(rc < 0)
perror("receive error\n");
close(sd);
exit(1);
buf[rc] = '\0';
if(strcmp(buf,"autoplay")==0)
//do something here
else if(strcmp(buf,"exit")==0)
printf("exiting now ....\n");
close(sd);
exit(1);
return 0;

Yes......I do so all the time.

Similar Messages

  • I have update my iphone 4 and my Mums they both use the same apple id and are both backed up to icloud now her contacts are on my phone.

    Hi I updated my iPhone 4 and my Mum's iPhone 4 to software version 5.0. They both use the same apple id and they are both backed up to iCloud. Once I updated my mum's phone i noticed that her contacts are now on my phone i deleted some of them but then she noticed that they had been deleted off her phone to. Has anyone got any ideas please. 

    Welcome to the Apple Community.
    Your password for logging into iCloud cant be different than your password for your Apple ID if you use that Apple ID to log into iCloud, it sounds as though you are using 2 accounts.

  • Can you track 2 iPhones if they are on the same apple I'd without using icloud

    Can you track 2 iPhones if they are on the same apple I'd without using icloud

    Well I have iCloud on mine but she has not updated her phone on hers but she can track me and not vice versa

  • Dear sir/madame,I have tried to use the inbrowser editing capability for Adobe Muse to login to my CMS. To login I have use the exact same FTP details I have used to upload my website, I even checked the page url multiple times but I keep getting the same

    Dear sir/madame,I have tried to use the inbrowser editing capability for Adobe Muse to login to my CMS. To login I have use the exact same FTP details I have used to upload my website, I even checked the page url multiple times but I keep getting the same error (the username and password are invalid for your FTP server. Please check them and try again). The hosting website (Yourhosting.nl) only has this one FTP user, which I cannot expand to more users. Can you please tell me if I am doing something wrong? The url to this page is http://e-divecollege.be/index.html or www.e-divecollege.be

    Dear sir/madame,I have tried to use the inbrowser editing capability for Adobe Muse to login to my CMS. To login I have use the exact same FTP details I have used to upload my website, I even checked the page url multiple times but I keep getting the same error (the username and password are invalid for your FTP server. Please check them and try again). The hosting website (Yourhosting.nl) only has this one FTP user, which I cannot expand to more users. Can you please tell me if I am doing something wrong? The url to this page is http://e-divecollege.be/index.html or www.e-divecollege.be

  • HT204380 Can you FaceTime between 2 iPads that are using the same apple ID but separate emails?

    Can you FaceTime between 2 iPads that are using the same apple ID but separate emails?

    Yes......I do so all the time.

  • TS4036 I purchased an iphone for my daughter and we are using the same account and she deleted some contacts from her phone and they were deleted off of my contacts as well. Is there a way to use icloud backup to reinstall the contacts to my phone?

    I purchased an iphone for my daughter and we are using the same account and she deleted some contacts from her phone and they were deleted off of my contacts as well. Is there a way to use icloud backup to reinstall the contacts to my phone?

    Welcome to the Apple community.
    You can only restore them from a Computer backup. This has occurred because you are sharing an iCloud account. ideally you should each have your own iCloud account, that way you can each manage your own mail, contacts, calendars, documents etc and avoid unintentional deletions and unwanted editing. If there is information you wish to share between you, this can be done with a secondary account.
    Having separate iCloud accounts, doesn't mean you have to have separate iTunes accounts, so whilst you keep your personal data separate, you can continue to share music, apps, books, TV shows, movies etc.

  • Is there a way to "mix" the nodes and leaves of the tree so if they are at the same level, they will display in a specified order.

    Is there a way to "mix" the nodes and leaves of the tree so that even if they are at the same level (1,2,3...), they will display in a specified order (via sort sequence, alphabetical, etc.).
    History:
    We are using the Tree UI element to display/manage a material bom interface. We seem to be running into an issue with displaying the nodes/leaves of the tree.. regardless of the order that the context is built (which is currently the order of the exploded BOM from from CS_BOM_EXPL_MAT_V2), the bom is displayed with the nested boms at the top of each level and the single materials below them. For example. If  TK1 contains Material1, Material2, Material3, Kit1(containing component1, comp2, comp3), Material4, Kit2(containing comp4, comp5, comp6), and Material5 (in this order), the tree will display with the A level node as TK1, the next node as Kit1 (with its subleaves of comp1,comp2,comp3), Kit2(with subleaves of comp4,comp5,comp6), THEN Material1, material2, material3, material4, material5.  Our users are adamant about the items displaying in the correct order (which should be alphabetical based on the description for one report and by location for purposes of inventory for another). I've searched but not been able to locate a similar question. If I've missed it, please point me in the right direction. The users want the tree,  not a "tree" table.  This is our first attempt at the tree, so maybe we're missing something basic?
    TK1
    -Mat1
    -Mat2
    -Mat3
    -Kit1
    --Comp1
    --Comp2
    --Comp3
    -Mat4
    -Kit2
    --Comp4
    --comp5
    --comp6
    -Material5
    displays as
    TK1
    -Kit1
    --Comp1
    --Comp2
    --Comp3
    -Kit2
    --Comp4
    --Comp5
    --Comp6
    -Mat1
    -Mat2
    -Mat3
    -Mat4
    -Mat5

    co-workers said example picture is misleading.. we can make the order work if everything is a "folder" but not a mix of "folders" and "files" (if making a visual reference to the windows browser). i.e - a file is represented as an empty folder.
    TK1    
    . Mat1
    . Mat2
    . mat3
    > kit1   
    .. comp1
    .. comp2
    .. comp3
    . mat4
    > kit2
    .. comp4
    .. comp5
    .. comp6
    . mat5
    displays at
    TK1
    > kit1
    .. comp1
    .. comp2
    .. comp3
    > kit2
    .. comp4
    .. comp5
    .. comp6
    . mat1
    . mat2
    . mat3
    . mat4
    . mat5
    we can make it work if everything is a folder. This is our current workaround.
    TK1
    > mat1
    > mat2
    > mat3
    v kit1 (when expanded)
    .. comp1
    .. comp2
    .. comp3
    > mat4
    > kit2 (when not expanded)
    > mat5

  • How can I use the same thread to display time in both JPanel & status bar

    Hi everyone!
    I'd like to ask for some assistance regarding the use of threads. I currently have an application that displays the current time, date & day on three separate JLabels on a JPanel by means of a thread class that I created and it's working fine.
    I wonder how would I be able to use the same thread in displaying the current time, date & day in the status bar of my JFrame. I'd like to be able to display the date & time in the JPanel and JFrame synchronously. I am developing my application in Netbeans 4.1 so I was able to add a status bar in just a few clicks and codes.
    I hope somebody would be able to help me on this one. A simple sample code would be greatly appreciated.
    Thanks in advance!

    As you're using Swing, using threads directly just for this kind of purpose would be silly. You might as well use javax.swing.Timer, which has done a lot of the work for you already.
    You would do it something like this...
        ActionListener timerUpdater = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                // DateFormat would be better, but this is an example.
                String timeString = new Date().toString();
                statusBar.setText(timeString);
                someOtherLabel.setText(timeString);
        new Timer(1000, timerUpdater).start();That code will update the time once a second. If you aren't going to display seconds, you might as well increase the delay.
    The advantage of using a timer over using an explicit thread, is that multiple Swing timers will share a single thread. This way you don't blow out your thread count. :-)

  • HT204053 my boyfriend and i are using the same apple account, i was wondering if there is a way for him to stop recieving txt msgs thats are sent to me through email accounts without removing him from my account altogether?

    my boyfriend and i are using the same apple account, i was wondering if there is a way to stop him recieveing txt msgs that are sent through email addresses without removing him from my account altogether?

    gemmie87 wrote:
    my boyfriend and i are using the same apple account, i was wondering if there is a way to stop him recieveing txt msgs that are sent through email addresses without removing him from my account altogether?
    Or remove you from his, it's moot because You and He are the same account, either get him his own (they are free) or live with this cozy but privacy compromised method.

  • Why am i getting this error? they are not the same?

    1021: Duplicate function definition.
    1021: Duplicate function definition.
    stop();
    import flash.events.MouseEvent;
    // home button definition \\
    // Portfolio button definition \\
    Portfolio_btn.addEventListener(MouseEvent.CLICK, pClick);
    function pClick(event:MouseEvent):void{
        gotoAndStop("getPortfolio");
    // Contact Me button defintion \\
    Contact_btn.addEventListener(MouseEvent.CLICK, cClick);
    function cClick(event:MouseEvent):void{
        gotoAndPlay("contactme");
    why am i getting this error the functions have different names... idk what to do they are for two different buttons I am using action script 3.0

    i did what u said and it said these two lines were the same
    // Portfolio button definition \\
    Portfolio_btn.addEventListener(MouseEvent.CLICK, pClick);
    function pClick(event:MouseEvent):void{                                                         <--------this one
        gotoAndStop("getPortfolio");
    // Contact Me button defintion \\
    Contact_btn.addEventListener(MouseEvent.CLICK, cClick);
    function cClick(event:MouseEvent):void{                                                           <--------and this one
        gotoAndPlay("contactme");
    it gave me the same error but they are not the same

  • Would the ir censor go bad if the hard drive cable went bad sens they bout use the same cable?

    what happend was that i have an intel ssd 80GB. i was installing a water colling system to my tower so i had to unplug my ssd. ones i had installed my water cooling sytem to my wotwer i pluged in my  sdd but it didnt work like if it had died. so i swipet out my macbooks hard drive for the ssd  to see if my macbook would reconise the hard drive in the utility but it didnt, and when i put my macbooks hard drive back in. the hard drive dosent get any power. you cant hear it turn on or anything. what i desisded to do is use the enclouser that you use for the optical drive and my mac boots up fine, could  my hard drive cable gone bad? if so why does the IR cencor work if they bouth use the same cable and end up in the same conector. i have a macbook pro mid 2009 5,5

    2009 MacBook Pros are notorious for having sub-standard SATA cables. I'm 99% certain that's your problem. Even though the IR sensor is on the cable, that doesn't mean that the cable is good - nor does it mean that it's not faulty if it will boot with a hard drive but not with a SSD - SSDs are simply more 'needy' when it comes to a good SATA cable.
    Visit www.ifixit.com and find the Apple part number and search for the item on eBay - much cheaper than buying from ifixit; although ifixit will have the instructions for installing the cable.
    Good luck,
    Clinton

  • HT204053 My wife and I are using the same apple ID. since last week, i am required to pay twice for the same app on our 2 devices (even though we are using the same ID). what can I do to fix this?

    My wife and I are using the same apple ID. Up until last week we used to buy an app and pay for it only once, but use it on both devices.
    since last week, the app store asks me to pay again for an app i already paid for on one device. On both devices automatic downloads (in settings) is and was always turned off.
    what can I do to fix this?

    Actually no. After installing an app on one device, I used to see an "install" button on the second device (since it's already been paid for). Now i get the full price button on the second device. I didn't want to proceed with the purchase because i didn't want to be billed twice.

  • I've downloaded some free games for my iphone4 but they are using my internet because they have ads. does anyone know if there are any games that don't have these ads and don't require internet AT ALL?and if i disable my internet connexion, does it help?

    i've downloaded some free games for my iphone4 but they are using my internet because they have ads. does anyone know if there are any games that don't have these ads and don't require internet AT ALL?and if i disable my internet connexion, does it help?

    Thank you. I put it in airplane mode like you suggested, but it looks to me like all the applications and ads are still running. Anyway, I'm just gonna play when I'm really really bored and use them as less as possible. Thank you again for your quick answer.

  • I have an apple ID which I use to sign into icloud for my iPad and iPhone.But when I use the same ID for setting up iCloud on my Macbook it says INCORRECT ID or password, try again. I tried changing my passwords but it does not work for the macbook.

    I have an apple ID which I use to sign into icloud for my iPad and iPhone.But when I use the same ID for setting up iCloud on my Macbook it says INCORRECT ID or password, try again. I tried changing my passwords several times but it does not work for the macbook.

    You will have to provide the correct password to delete the existing account, if you have tried but are not getting the password reset email, contact Apple for assistance by going to https://expresslane.apple.com, then click More Products and Services>Apple ID>Other Apple ID Topics>Lost or forgotten Apple ID password.

  • HT4864 How can I change my iCloud email to my AppleID email they are not the same

    How can I change my iCloud email to my AppleID email?  They are not the same and somehow were switched by my teenage daughter.  I can access the account and change anything except my iCloud eMail account.

    Hi itsjeffb,
    If I am understanding you correctly, it sounds like your daughter may have set up an alias account under iCloud. Your AppleID email address may or may not be the same as you iCloud email address, depending on how it was set up. The following articles provide more information about iCloud email addresses and how aliases work under iCloud:
    iCloud: About your icloud.com, me.com, and mac.com email addresses
    http://support.apple.com/kb/HT2623
    iCloud: Using your @icloud.com email address
    http://support.apple.com/kb/HT5441
    iCloud: Create or change email aliases
    http://support.apple.com/kb/PH2622
    Cheers,
    - Brenden

Maybe you are looking for

  • Oracle IDS Version problem (IDS is not compatible )

    Please address my technical query mentioned below. We are facing problems while porting/deploying our application on the Oracle 10g application server. The application server version is 10g R-2 (64bit)(10.1.2.0.2), the IDS version is 10g (9.0.4) and

  • Need to move my applications to new OS and Hard Drive, but Tiger doesn't have Time Machine, right?

    I updated my RAM, put in a new hard drive, and updated my OS from tiger to snow leopard (no I'm NOT ready for lion) all at the same time on my 2007 MacBook. Though I backed up general files on my hard drive, I didn't do a full system backup, and now

  • How do I reduce file size in iMovie

    I took a few videos I made with my iPhone, stitched them together and addes some music... when I made export to QuickTime, the files is huge and prevents me from uploading into wordpress and unreasonable to load anywhere else (youtube says it encount

  • Invoke a from from another form

    Hi, Is it possible to invoke a form from another form. For example, based on a radio button choice or in button click event, i just need to show another form, but not in seperate window. We tried using app.launchURL(form workspace url). However it do

  • Download from camera to iPhoto or Photos

    Until yesterday I could download the photos on my digital camera to iPhoto. My Mac has now done an update & installed Photos & when I plug the sync lead in from the camera it shows the new photos in the preview pane but when I try to download I get a