![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 5
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
Where is your code?
__________________
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 5
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2005
Posts: 5
Rep Power: 0
![]() |
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());}
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|