• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

making Thread of methods

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




This is a simple prototype of my application the same is the structure of my program...
I want to use two methods ABC and MNO...
And both methods I want to use as THread...
It is a syntactical error if we define

how can I overcome with the problem ...
Please help me...


[HENRY: Added Code Tags]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters, the code should not extend Thread, it should implement Runnable: ExtendingThreadVsImplementingRunnable

What do you mean by "syntactical error"? The code compiles and runs fine for me.

Method MNO will of course never be called, because method ABC never returns.

As an aside, in the future please UseCodeTags when posting code of any length for the reasons stated in that page.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(new Thread(new Test.ABC())).start();
how can I overcome with the problem ...



You can't just make up new Java syntax when you want something new. You need to modify the class to do what you want. Possible choices are.... Create a new (and different) test classes that calls the different methods -- and start a new thread for each in main. Or make the test class configurable, via a constructor -- and start a new thread for each in main.

Henry
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HENRY::--


I guess the code will not be required in my case but still I will add that

I am talking this in context of Client.java

I am using 4 classes in my exact program. First class is main
2nd:- is initiating socket connection, which are further used by both other classes..
3rd:- This class is meant for Catching the keys pressed or input given..Then use in its interface and sends to server as well..
4th:- This is meant to recieve Data from the server..

Now for this I have tried different structures in my program but in each and every I failed..
I want to run both simultaniously(Recieving and sending)
Thus I have planned to use thread for this..

If I use single class then only a single run method will be there and won't be able to run 2 methods.
So I have planned to use 2 classes.
My code is as follows

Client.java

import java.io.*;
import java.net.*;

public class Client
{
public static void main(String[] s)
{
CreateSocket CCS = new CreateSocket();
Recieve RC = new Recieve();
ClientSend CSend = new ClientSend();
try
{
CCS.makeConn();
(new Thread(RC)).start();
(new Thread(CSend)).start();
}
catch(IOException e)
{
System.err.println("There is some error");
}
//CCS.data();
}
}

class CreateSocket
{
PrintWriter out = null;
BufferedReader in, stdIn = null;
Socket CSocket = null;
//BufferedReader Stdin = new BufferedReader(new InputStreamReader(System.in));
public void makeConn() throws IOException
{

try
{
CSocket = new Socket("127.0.0.1", 4141);
}
catch(UnknownHostException e)
{
System.err.println("Could not Find Localhost");
}
catch(IOException e)
{
System.err.println("Could not Make IO connection to localhost");
}
}
}

class Recieve extends CreateSocket implements Runnable
{
public void sendData()
{
String toServer,fromServer = null;
try
{
in = new BufferedReader(new InputStreamReader(CSocket.getInputStream()));
out = new PrintWriter(CSocket.getOutputStream(), true);
stdIn = new BufferedReader(new InputStreamReader(System.in));
while((toServer=stdIn.readLine()) != null)
{
System.out.println("Client: "+toServer);
out.print(toServer);
toServer = stdIn.readLine();
}
}
catch(IOException e)
{
System.err.println("Some Unreported IOException");
}
}
public void run()
{
sendData();
}
}

class ClientSend extends CreateSocket implements Runnable
{
public void data()
{
String fromServer;
try
{
in = new BufferedReader(new InputStreamReader(CSocket.getInputStream()));
while((fromServer=in.readLine()) != null)
{
if(fromServer == "Quit")
{
break;
}
System.out.println("Server : " + fromServer);

}
}
catch (IOException e)
{
System.err.println("This is an error");
}
}

public void run()
{
data();
}
}



Server.java

import java.net.*;
import java.io.*;

public class Server
{
public static void main(String[] s)
{
CreateSocket CS = new CreateSocket();
////CS.sendData();

try
{
CS.makeConn();
Thread t1 = new Thread(CS);
t1.start();
}
catch(IOException e)
{
System.err.println("There is some problem");
}
}
}

class CreateSocket implements Runnable
{
ServerSocket serverSocket = null;
Socket clientSocket= null;
public void makeConn() throws IOException
{
try
{
serverSocket = new ServerSocket(4141);
}
catch(IOException e)
{
System.err.println("Port 4444 doesn't seem to be opened");
System.exit(1);
}

try
{
clientSocket= serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept Failed");
}
}

PrintWriter out=null;
public void sendData()
{
String outLine = null;
try
{
BufferedReader stdIn = null;
PrintWriter out = new PrintWriter(clientSocket.getOutputStream() , true);
outLine = "This is send by server";
out.println(outLine);
stdIn = new BufferedReader(new InputStreamReader(System.in));
while(outLine != "Quit")
{
outLine = stdIn.readLine();
if(outLine != null)
{
System.out.print("\r\n Server: "+ outLine);
out.println(outLine);
}

}
}
catch(IOException e)
{
System.err.println("Some Problem");
}
}

//public void recieveData()
//{
// String inLine = null;
//}

public void run()
{
sendData();
}
}
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you go back and edit your post and put code tags around the code? The edit button is on the top right corner of the post. Then highlight the code portion of the post and press the [Code] button found above the top of the text box.

Also, you said "Now for this I have tried different structures in my program but in each and every I failed.." Can you tell us what the problem you are having with the posted code? "It failed" isn't very descriptive and providing more details may help us diagnose the problem.
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geeks you are not getting my problem. I am having problem with client in running two diffrent tasks. One for recieving message and the other for sending message.
Can we run 2 Threads over a single class object, with condition of each thread only caring for a single method.
For example if we have 2 methods A() and B()
and 2 Threads t1 and t2
Then is it possible if A() and B() are methods of a single class and t1 cares for A() and t2 cares for B()
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is my simple code in this I have not to use
and how can I make both method accessible
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

V. Garg wrote:Geeks you are not getting my problem. I am having problem with client in running two diffrent tasks. One for recieving message and the other for sending message.
Can we run 2 Threads over a single class object, with condition of each thread only caring for a single method.
For example if we have 2 methods A() and B()
and 2 Threads t1 and t2
Then is it possible if A() and B() are methods of a single class and t1 cares for A() and t2 cares for B()



I thought that I answered this question already, in my first post...

You need to modify the class to do what you want. Possible choices are.... Create a new (and different) test classes that calls the different methods -- and start a new thread for each in main. Or make the test class configurable, via a constructor -- and start a new thread for each in main.



If you explain why this answer is not what you want, maybe we can understand why are we "not getting your problem".


[BTW, although I don't really care, some people consider being called a "geek", instead of using their actual names derogatory. Doing so is a great way of turning people off from helping you.]

Henry
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANKS for your efforts..The problem is solved.
and the code is


 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This solution apparently works, but is rather ugly, and I wouldn't trust it if I were you. There are much better tools available to do what you want. One way I like best is the use of anonymous inner classes. Pass a new Runnable to the Threads that call the method you want. Something like this:

 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for giving me new stretigy...
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends I am hacing an unobvious problem here in my code and I don't know how to overcome with this. I have spended a lot of my time in fiding the problem but didnn't get success.
Please anyone help me.

The problem
when I connect my first client to server it is working correctly
but if i start another client the working fails
that is like server don't sends any message to second client neither it accepts any message from it




Server.java






Client.java



 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see provisions in the code for accepting more than a single client. You should work through http://java.sun.com/docs/books/tutorial/networking/sockets/index.html, particularly the "Writing a Client/Server Pair" section.

Also, " outLine != "Quit" " won't work - string equality is tested using the "equals" method, not with the "!=" operator.
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After running the code you will see that it is accepting each and every client. I have also made a little change in the code. But still thee is only the problem with text sharing with second client and server.. Only the First client is woring accurately. and any other client after the first one is not working according to the expectation.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One lingering problem I see is the one described in Don't println to a Socket.

Also, you still have those buggy " == "Quit" " and " != "Quit" " statements in the code.
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
brothers i know that I am using println in my code.
But that is working with the first client. Only the problem is occuring with the second client and not with the first one.
It should work as it is working wiht the first client...
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can tell that you haven't yet read the link I provided for you, especially not the section "Supporting Multiple Clients". So what I wrote earlier is still true:

I don't see provisions in the code for accepting more than a single client.

 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf Dittmer:- I have read and implemented. It is also working in correct manner. It is accepting more than one client. The link provided by you also tells about the same thing. and in thta aspect my code is also working. It is failing only in accepting and sending data to and from server.
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried a practical on my code by printing diffrent values in my cade and finally I see that my code is not entering in these while blocks
These both while are of Client.java

AND
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is accepting more than one client.


Then you should post the code that does that. The code shown above does NOT accept more than one client.
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the same code.
If Server will not accept more than single client then the second client will die or better to say that running of the second client will stop. But here is not the case. This means that the second client is running.

I have used

And run every request in another thread
This will accept all the clients which will come to the server.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And run every request in another thread
This will accept all the clients which will come to the server.



The code is neither accepting more than a single client ("accept" is called only a single time), nor are you creating new threads on the server to deal with the client; both those are mentioned in the paragraph "Supporting Multiple Clients". I suggest to study the example codes given there so that you get a solid understanding on how ServerSocket is used for creating multi-threaded servers.
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brother I have edited this code after you have made your earlier comment.
Am i wrong in this one also
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brother I have edited this code after you have made your earlier comment.




The code shown above still has all the problems I mentioned so far: it uses println with a socket, it uses "==" and "!=" for string comparisons, it doesn't accept more than one client, and it doesn't start separate threads for clients. So I'm not sure what you mean by "I have edited this code".

Why don't you post the code you have now in a new post, so that we're all on the same page?
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What change in code then should I made...I am not getting any option that I need to change in my code...I have gone through the tutorial you have mentioned But still aftre reading that I didn't find any appropriate solution
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "gone through the tutorial"? Have you extracted the source code of the example client and server and gotten those to run? Have you found the spot in the server code that accepts multiple clients and spawns off new threads? Do you understand how that code is doing what your code does not?
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I have tried to implement that code and finally reach to a change



Will it allow more than one Client.

Or I should remove the from the main method
and use this in while true in makeconn()
and call makeConn() from main
by


What do you think will allow more client
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you understand enough about Java threads yet (how they work, what their lifecycle is, etc.) to make this work. I suggest you work through the thread section of the Java Tutorial first. Then it might be easier to understand the client/server example.
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okie bro thanks alot for your contribution and help. I will really try to study more. But still will request you if you could suggest me the right option from the 2 which I have posted in my last post.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neither of those two will work; you'll need to make a lot more changes.
 
V. Garg
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the task is done...
Thanks Ulf Dittmer for prividing tutorial and guiding about to study more. Same was done by me.
The tutorial provided by you or better to say sample code provide by you was also helpfull.

There is still a problem that the server is having only a single Text sending thread. Due to this there only a single random client recieves dtaa from the server and not all.
Now I am trying to find out what the problem is.....
My code is as follows

Sever.java


And the client part is as follows
Client.java





Will anyone please guide me what change should I make in my code.

Thanking You:-
Vikas Garg
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic