|
If you're using blocking socket calls then you need a thread per client, and a thread for the server (if the server is a console application which does nothing else then that can be the main thread). accept and recv both block indefinitely so they need to be in separate threads. (send also blocks, but only for a short time so you don't need to worry about that in my experience.)
You can use java.nio.* for asynchronous sockets. I've never used it but there are some examples here[^] and searching will turn things up.
|
|
|
|
|
Thinking it over again, a single threaded non-blocking solution might be easier to implement than a multithreaded blocking if someone has no experience with threading. I had a multithreaded async reactor pattern in my mind that put the async solution to a more difficult level than the blocking one...
EDIT: I think the OP should try to write both solutions to get the feeling of it if he is interested!
|
|
|
|
|
What is a non blocking solution, how would it work?
|
|
|
|
|
|
OKAY so here comes the update, I've added it into threads but now a unknown error occurs. Thanks to a debugger I know where and when it occurs but I don't know why. I get a exceptionNullPointer at line 11 of Users.java
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
import java.net.*;
import java.io.*;
public class Room implements Runnable{
public void run(){
ServerSocket serversocket;
Socket socket;
Clients client = new Clients();
try{
serversocket = new ServerSocket(7776);
while(true){
socket = serversocket.accept();
System.out.println("Client connected" + socket);
client.newThread(socket);
}
}catch(IOException e){
System.out.println(e);
}
}
public static void main(String[] args){
(new Thread(new Room())).start();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.net.Socket;
public class Clients implements Runnable{
Socket socket1;
public void run(){
Socket socket2 = socket1;
Users userfunction = new Users();
BufferedReader bf;
bf = userfunction.addStream(socket2);
while(true){
try{
String out = bf.readLine();
System.out.println(out);
if(out != null)
userfunction.sendMessageALL(out);
}catch(IOException e){
System.out.println(e);
}
try{
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
public void newThread(Socket socket){
socket1 = socket;
(new Thread(new Clients())).start();
}
}
import java.net.*;
import java.io.*;
import java.util.ArrayList;
public class Users {
ArrayList<BufferedReader> recieveAL = new ArrayList<BufferedReader>();
ArrayList<PrintWriter> sendAL = new ArrayList<PrintWriter>();
public BufferedReader addStream(Socket socket){
BufferedReader br;
PrintWriter pw;
try{
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(), true);
recieveAL.add(br);
sendAL.add(pw);
return br;
}catch(IOException e){
e.printStackTrace();
}
return null;
}
public void sendMessageALL(String send){
if(send != null){
for(int x = 0; x < sendAL.size(); x++){
PrintWriter pw = sendAL.get(x);
pw.println(send);
pw.close();
}
}
}
public void sendMessage(String send,int x){
PrintWriter pw = sendAL.get(x);
pw.println(send);
pw.close();
}
}
|
|
|
|
|
probably because there is a null pointer there. why don't you debug your code?
|
|
|
|
|
A null pointer is when it returns a Null, right?
I don't see the problem because that part of the code is unchanged. so why return an error now
|
|
|
|
|
You have a serious bug in your newThread() method that causes the nullpointer exception.
|
|
|
|
|
Parsing the Socket into the thread was my screw up, so It was replaced with parameters of the thread.
|
|
|
|
|
OK, here are a few suggestions:
1. I see you are coming from C because you declare all your function local variables at the beginning of the functions (sorry, methods). Declare your variables where you use it, or at least most most-inner scope where you need it.
2. Room does not have to implement the Runnable interface and you dont have to create a new thread for the room in your main(), call the run() method directly and execute it with the main thread.
3. The biggest mistake that causes the nullpointer is in Client.newThread: you should pass in this instead of new Clients(). I would do the work of Client.newThread in the constructor of Client and would create Client only after accepting the socket.
|
|
|
|
|
I really don't understand, this is currently beyond me at the moment.
|
|
|
|
|
Well, object oriented programming is not easy at first. Socket/network programming is also hard. Threading is another beast. And you are mixing all of these, are you sure this is what you wanna do?
|
|
|
|
|
I'm committed to learn it and since I'm self taught, I've decided to throw myself in and learn how it all works then learn in more depth.
|
|
|
|
|
I don't recommend doing so, but good luck!
|
|
|
|
|
how to make session in beanshell ??
|
|
|
|
|
Please try a better title, and a clearer question. You should first try typing this question into Google to see what you get.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
jsp里边有一个内置的对象叫做session,不需要创建直接用就行,调用方法比如session.setAttribute("name","value");
you needn't create but it exeists.
|
|
|
|
|
Hi,
i have a main webpage with this code
<input type="button" value="Maak een bord positie code" onClick="window.open('makeboard.html','makeposition',' width=500,height=700');">
my form is, i think well formed
<form name="post" action="add.php" method="post" name="myform" id="myform"> and also closed
On the child form I have some code
var pa=parent.document.getElementById('myform')
However the var pa is empty.
has someone an idee what is wrong?
Jan
|
|
|
|
|
Are you sure this is java? Looks more like javascript.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Your right. It's javascript. I didn't know there is another forum for javascript. However I found it after days of looking I posted the question and today I found that I must use 'window.opener.document'
Jan
PS: If there is an admin he/she may move the message to the correct forum
|
|
|
|
|
Okay. The admins may or may not move this, it depends on getting them sober enough for long enough.
BTW, I did not 1 vote your question, I will counter it as it is not deserved.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
hello
I wanna make session between two field in code .bsh (BeanShell) : is sample java code
Internet pages contain the same field Example: Field Name
When entering values in the first field in the first page and move on to the second page must be entered the same values found in the other the right
please i want that code ???
|
|
|
|
|
I want the code to generate everything I need for the project I'm working on, but that doesn't mean I'll get it.
Try to rephrase the question as a first step to make it clearer (I don't understand very much of what you're asking to be honest). And as a second step, try at least to come up with a solution all by yourself, post what you've come up with, and where you got stuck. People will try to help you, but only if you show that you're willing to help yourself.
In the current form of your question, you're highly unlikely to get an answer.
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
modified 2-Aug-12 7:03am.
|
|
|
|
|
Hai,can any one help me in this querry.
I am calling a function 'A' which is in a class 'CA', from a function 'B' which is in the Class 'CB'.If there is any exception produced in function 'A' it will generate an error code(intiger Value)in Catch block.How can i get that error code from the Function A to function B only if the exception had occured.
|
|
|
|
|
If the error code generated by method A on exception is unique (meaning that no normal output from A will ever match that error code), you can simply do this (assuming that error code returned on exception is -1):
if(ca.A() == -1) {
}
Conversely, if you have access to the sources and you are allowed to modify them, don't catch the exception. Modify the CA.A() method signature and add throws ExceptionThrownFromCA (just an example). Like this, you don't even need to check the error code, and you can do this:
public void B() {
CA ca = new CA();
try {
ca.A();
} catch(ExceptionThrownFromCA e) {
}
}
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
modified 1-Aug-12 10:07am.
|
|
|
|