Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 4th, 2005, 8:49 AM   #1
captainK
Newbie
 
Join Date: Feb 2005
Posts: 5
Rep Power: 0 captainK is on a distinguished road
Angry Multiple server clients using sockets + Threads

Hi,

Im trying to get a Java server application working that i have written to run multiple client threads. I need multiple threads because each of the two server threads uses a TCP/IP socket to make a connection to their respective client.

The problem is though that i need to pass integer and float data between the two server threads, and have tried a few different ways of doing this but without success.

Any suggestions would be gratefully received as i am working on a tight schedule now and need to sort this within the next week.

Thanks,
Phil.
captainK is offline   Reply With Quote
Old Mar 4th, 2005, 8:55 AM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Where is your code?
__________________

tempest is offline   Reply With Quote
Old Mar 4th, 2005, 9:19 AM   #3
captainK
Newbie
 
Join Date: Feb 2005
Posts: 5
Rep Power: 0 captainK is on a distinguished road
Multiple server clients using sockets + Threads

Hi,

Thanks for the prompt reply.

There are three code files attached to this post - "threadExs" is the parent file and calls the other "SimpRunnable" and "SimpThread" to run the two threads. I haven't yet integrated all of my code into the "SimpRunnable" file but here it is in its current state.

Thanks,
Phil.
Attached Files
File Type: zip Java_Programs.zip (1.7 KB, 36 views)
captainK is offline   Reply With Quote
Old Mar 4th, 2005, 11:10 AM   #4
captainK
Newbie
 
Join Date: Feb 2005
Posts: 5
Rep Power: 0 captainK is on a distinguished road
Multiple server clients using sockets + Threads

And, for those of you who dont like attachments, heres the code:

//MAIN PARENT CLASS
import java.io.*;

class threadExs
	{
	public static void main(String[] a)
		{
		SimpThread t1 = new SimpThread();
		t1.start();

		Thread t2 = new Thread (new SimpRunnable());
		t2.start();

		System.out.println("Value returned to parent program: " + SimpThread.GetLevelActualValue());
	}
}



//FIRST CHILD CLASS
import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.lang.*;

class SimpThread extends Thread
	{
	//int i;
	//   1.  extend class java.lang.Thread
	static String [] Time=new String[5000];
	static String [] LP=new String[5000];
	static String [] LI=new String[5000];
	static String [] LD=new String[5000];
	static String [] LA=new String[5000];
	static String [] LS=new String[5000];
	static String [] TP=new String[5000];
	static String [] TI=new String[5000];
	static String [] TD=new String[5000];
	static String [] TA=new String[5000];
	static String [] TS=new String[5000];
	static String [] FP=new String[5000];
	static String [] FI=new String[5000];
	static String [] FD=new String[5000];
	static String [] FA=new String[5000];
	static String [] FS=new String[5000];
	static String [] FO=new String[5000];
	static String [] TO=new String[5000];
	static  int i=0;
	ServerSocket serverSocket = null;


	public void run()
		{
		//for(i = 0; i < 300; i++)// 0-299 printed
		//System.out.println(i + ". Extension of THREAD running");

		try
			{
			serverSocket = new ServerSocket(1002);
		}
		catch (IOException e)
			{
			System.err.println("Could not listen on port: 1002.");
		}

		Socket clientSocket = null;
		System.out.println("Waiting for PCU software to start...");
		try
			{
			clientSocket = serverSocket.accept();
		}
		catch (IOException e)
			{
			System.err.println("Accept failed.");
		}
		System.out.println("PCU software running.");
		System.out.println("Data returned from PCU:");
		while (true)
			{
			try
				{
				PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
				BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream()));
				String ss=in.readLine();

				Time[i]= (ss.substring(ss.indexOf("T")+1, ss.indexOf("LP")));
				LP[i]=(ss.substring(ss.indexOf("LP")+2, ss.indexOf("LI")));
				LI[i]=(ss.substring(ss.indexOf("LI")+2, ss.indexOf("LD")));
				LD[i]=(ss.substring(ss.indexOf("LD")+2, ss.indexOf("LA")));
				LA[i]=(ss.substring(ss.indexOf("LA")+2, ss.indexOf("LS")));
				LS[i]=(ss.substring(ss.indexOf("LS")+2, ss.indexOf("FS")));
				FS[i]=(ss.substring(ss.indexOf("FS")+2, ss.indexOf("FA")));
				FA[i]=(ss.substring(ss.indexOf("FA")+2, ss.indexOf("FP")));
				FP[i]=(ss.substring(ss.indexOf("FP")+2, ss.indexOf("FD")));
				FD[i]=(ss.substring(ss.indexOf("FD")+2, ss.indexOf("FO")));
				FO[i]=(ss.substring(ss.indexOf("FO")+2, ss.indexOf("TS")));
				TS[i]=(ss.substring(ss.indexOf("TS")+2, ss.indexOf("TA")));
				TA[i]=(ss.substring(ss.indexOf("TA")+2, ss.indexOf("TP")));
				TP[i]=(ss.substring(ss.indexOf("TP")+2, ss.indexOf("TI")));
				TI[i]=(ss.substring(ss.indexOf("TI")+2, ss.indexOf("TD")));
				TD[i]=(ss.substring(ss.indexOf("TD")+2, ss.indexOf("TO")));
				TO[i]=(ss.substring(ss.indexOf("TO")+2, ss.length()));

				System.out.println("  "+Time[i]+"  "+ LP[i]+"  "+LI[i]+" "+LD[i]+" "+LA[i]+" "+ LS[i]+" " + FA[i]+" "+FO[i]);
				i++;
			}
			catch(IOException e){}
			try
				{
				Thread.sleep(1000);
			}
			catch(Exception e){}
		}//end while
	}//end run()

	public static String GetLevelActualValue(){return  LA[i];};
}//end class



//SECOND CHILD CLASS
import java.io.*;

class SimpRunnable implements Runnable
	{
	int i;
	//   2.  implement the Runnable interface,
	public void run()
		{
		for(i = 0; i < 300; i++)  // 0-299 printed
		{try{Thread.sleep(1000);}catch(InterruptedException e){}
		System.out.println(i + ". Implem. of RUNNABLE running");
		System.out.println("Value returned to SimpRunnable program: " + SimpThread.GetLevelActualValue());}
	}
}
captainK is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC