| | |
Multithreading.....Help pls
![]() |
•
•
Join Date: Mar 2006
Posts: 24
Reputation:
Solved Threads: 0
Client side:
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String[] args) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in));
while(true)
{
Socket clientSocket = new Socket("hostname", 5678);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
//clientSocket.close();
}
}
}
----------------------------------------------------------------------------------------------------------
Server side:
import java.io.*;
import java.net.*;
class TCPServer
{
public static void main(String[] args) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(5678);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
I have read these codes in my textbook. It works. But in this given source code, i want it to change to the multithreading.
What should I add to my source code.
Thank you so much, I really need it.
bagi
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String[] args) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in));
while(true)
{
Socket clientSocket = new Socket("hostname", 5678);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
//clientSocket.close();
}
}
}
----------------------------------------------------------------------------------------------------------
Server side:
import java.io.*;
import java.net.*;
class TCPServer
{
public static void main(String[] args) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(5678);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
I have read these codes in my textbook. It works. But in this given source code, i want it to change to the multithreading.
What should I add to my source code.
Thank you so much, I really need it.
bagi
What do you know about threads?
What exactly about adding threading to this program do you not understand?
After each connection, you need to start a new thread using the socket created by the connection. What else do you need to know?
If you don't know how to make and/or start the thread, then please consult the threading tutorials at Sun first, then come back and try this.
What exactly about adding threading to this program do you not understand?
After each connection, you need to start a new thread using the socket created by the connection. What else do you need to know?
If you don't know how to make and/or start the thread, then please consult the threading tutorials at Sun first, then come back and try this.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- windows xp installation error!! pls help! (Troubleshooting Dead Machines)
- multithreading & destructor call. (C++)
- pls.help me im begging you!!! (C)
- pls... i need help!!! (C)
- "Problem in MultiThreading" (Java)
- Multithreading problem (C)
- Pls Hlp me with Hijackthis log file. (Viruses, Spyware and other Nasties)
- Pls help with my HJT log (Viruses, Spyware and other Nasties)
- can somebody pls. help me out with my HJT log.. (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: SOAP-XML-Exceptions
- Next Thread: insert pictures and movies in oracle9i using jdbc
| Thread Tools | Search this Thread |
addball android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) button card class classes client code collision columns component constructor crashcourse css database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image integration intellij j2me java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle phone physics plazmic print problem program programming project radio scanner server service set sharepoint smart sms smsspam software sql subclass support swing textfield threads tree trolltech unlimited utility windows






