freelanceprogrammers.org Forum Index » Java

Server question


View user's profile Post To page top
msulli1355 Posted: Sat Apr 17, 2004 5:50 am


Joined: 17 Apr 2004

Posts: 28
Server question
I`m trying to write a java server than an unlimited number of clients
will be able to be connected to at the same time. I looked at several
sites about how to do this. The closest site I found to what I was
looking for hinted to use threads. I`ve never really used threads
before. How would I accomplish this? I`ve included my code below.
-Michael Sullivan-

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

public class server {

public ServerSocket serverSocket = null;
public Socket clientSocket = null;
public server() throws IOException
{
try
{
serverSocket = new ServerSocket(9999);
}
catch (IOException e) {}

try
{
clientSocket = serverSocket.accept();
}
catch (IOException e){}

PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;

outputLine = "Hello.";
out.println(outputLine);


do
{
inputLine = in.readLine();
outputLine = inputLine;
if (inputLine.equalsIgnoreCase("Bye"))
break;
out.println(outputLine);
System.out.println("Client typed: " + inputLine);
}
while (inputLine != null);

out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
public static void main(String[] args) throws IOException
{
int infinity = 0;
while (infinity == 0) { new server(); }
}
}
Reply with quote
Send private message
View user's profile Post To page top
jreidthompson Posted: Sat Apr 17, 2004 8:02 am


Joined: 15 Feb 2002

Posts: 6
Server question
http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html#
multi

http://www.ryerson.ca/~dgrimsha/courses/cps841/javaApps/servers/boringServer/Bor
ingServer.java

http://www.ase.md/~aursu/ClientServerThreads.html

http://hacktor.fs.uni-bayreuth.de/~dun3/usa-data/java-www/A%20Simple%20Multithre
aded%20Web%20Server.htm

Michael Sullivan wrote:

> I`m trying to write a java server than an unlimited number of clients
> will be able to be connected to at the same time. I looked at several
> sites about how to do this. The closest site I found to what I was
> looking for hinted to use threads. I`ve never really used threads
> before. How would I accomplish this? I`ve included my code below.
> -Michael Sullivan-
>
> import java.net.*;
> import java.io.*;
>
> public class server {
>
> public ServerSocket serverSocket = null;
> public Socket clientSocket = null;
> public server() throws IOException
> {
> try
> {
> serverSocket = new ServerSocket(9999);
> }
> catch (IOException e) {}
>
> try
> {
> clientSocket = serverSocket.accept();
> }
> catch (IOException e){}
>
> PrintWriter out = new
> PrintWriter(clientSocket.getOutputStream(), true);
> BufferedReader in = new BufferedReader(
> new InputStreamReader(
> clientSocket.getInputStream()));
> String inputLine, outputLine;
>
> outputLine = "Hello.";
> out.println(outputLine);
>
>
> do
> {
> inputLine = in.readLine();
> outputLine = inputLine;
> if (inputLine.equalsIgnoreCase("Bye"))
> break;
> out.println(outputLine);
> System.out.println("Client typed: " + inputLine);
> }
> while (inputLine != null);
>
> out.close();
> in.close();
> clientSocket.close();
> serverSocket.close();
> }
> public static void main(String[] args) throws IOException
> {
> int infinity = 0;
> while (infinity == 0) { new server(); }
> }
> }
>
>
>
> ------------------------------------------------------------------------
> *Yahoo! Groups Links*
>
> * To visit your group on the web, go to:
> http://groups.yahoo.com/group/beginnersclub/
>
> * To unsubscribe from this group, send an email to:
> beginnersclub-unsubscribe@yahoogroups.com
> <mailto:beginnersclub-unsubscribe@yahoogroups.com?subject=Unsubscribe>
>
> * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
> Service <http://docs.yahoo.com/info/terms/>.
>
>
Reply with quote
Send private message
View user's profile Post To page top
msulli1355 Posted: Sat Apr 17, 2004 8:06 am


Joined: 17 Apr 2004

Posts: 28
Server question
Thank you.

-Michael Sullivan

On Fri, 2004-04-16 at 22:02, Reid Thompson wrote:
>
http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html#
multi
>
>
http://www.ryerson.ca/~dgrimsha/courses/cps841/javaApps/servers/boringServer/Bor
ingServer.java
>
> http://www.ase.md/~aursu/ClientServerThreads.html
>
>
http://hacktor.fs.uni-bayreuth.de/~dun3/usa-data/java-www/A%20Simple%20Multithre
aded%20Web%20Server.htm
>
> Michael Sullivan wrote:
>
> > I`m trying to write a java server than an unlimited number of
> clients
> > will be able to be connected to at the same time. I looked at
> several
> > sites about how to do this. The closest site I found to what I was
> > looking for hinted to use threads. I`ve never really used threads
> > before. How would I accomplish this? I`ve included my code below.
> > -Michael Sullivan-
> >
> > import java.net.*;
> > import java.io.*;
> >
> > public class server {
> >
> > public ServerSocket serverSocket = null;
> > public Socket clientSocket = null;
> > public server() throws IOException
> > {
> > try
> > {
> > serverSocket = new ServerSocket(9999);
> > }
> > catch (IOException e) {}
> >
> > try
> > {
> > clientSocket = serverSocket.accept();
> > }
> > catch (IOException e){}
> >
> > PrintWriter out = new
> > PrintWriter(clientSocket.getOutputStream(), true);
> > BufferedReader in = new BufferedReader(
> > new InputStreamReader(
> > clientSocket.getInputStream()));
> > String inputLine, outputLine;
> >
> > outputLine = "Hello.";
> > out.println(outputLine);
> >
> >
> > do
> > {
> > inputLine = in.readLine();
> > outputLine = inputLine;
> > if (inputLine.equalsIgnoreCase("Bye"))
> > break;
> > out.println(outputLine);
> > System.out.println("Client typed: " + inputLine);
> > }
> > while (inputLine != null);
> >
> > out.close();
> > in.close();
> > clientSocket.close();
> > serverSocket.close();
> > }
> > public static void main(String[] args) throws IOException
> > {
> > int infinity = 0;
> > while (infinity == 0) { new server(); }
> > }
> > }
> >
> >
> >
> >
> ------------------------------------------------------------------------
> > *Yahoo! Groups Links*
> >
> > * To visit your group on the web, go to:
> > http://groups.yahoo.com/group/beginnersclub/
> >
> > * To unsubscribe from this group, send an email to:
> > beginnersclub-unsubscribe@yahoogroups.com
> >
> <mailto:beginnersclub-unsubscribe@yahoogroups.com?subject=Unsubscribe>
> >
> > * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
> > Service <http://docs.yahoo.com/info/terms/>.
> >
> >
>
> Yahoo! Groups Sponsor
> ADVERTISEMENT
> click here
>
>
> ______________________________________________________________________
> Yahoo! Groups Links
> * To visit your group on the web, go to:
> http://groups.yahoo.com/group/beginnersclub/
>
> * To unsubscribe from this group, send an email to:
> beginnersclub-unsubscribe@yahoogroups.com
>
> * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
> Service.
>
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help