Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help in creating a Concurrent Server  RSS feed

 
Brenda Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a java HTTP server program, but now i need to add some more functionality to it. I need to make it into a concurrent server using a separate thread for each processing each client request and im not quite sure how to do this. help in how to do this would be much appreciated
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you had a look a the java.util.concurrent package?
 
Brenda Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have but im slightly still consufed. I have an example of code for a concurrent server but im not sure how to implement it into the code that I already have created for the HTTP Server.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumable your server has some sort of entry point? A thread that is waiting for HTTP requests? Well, that class needs to be creating new threads for each request. Make sense?
 
Brenda Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically at the moment all my server does is:


1. read the client request
2. extract important bits of data from the request, for example:
the command (usually GET), the resource name requested, and
other bits,
3. lots of checking and if necessary return error responses to the
client, for example: check the command is legal or send 501,
check the request is properly structured or send 400,
4. convert the resource name to a file name and check the file exists
or send 404,
5. if ok, send correct response and option lines to the client and
then open the file, read it and send the contents to the client after
the response lines,
6. close the client connection
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, presumably it till still need to do all this. You just needs to delegate the whole operation out to distinct threads don't you? So your server would now:
1. wait for client request
2. when one arrives, start a thread
3. thread does the rest.
 
Brenda Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes that is correct.
 
James george
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yeah im also starting to learn java and some information on how to go about creating a concurrent server would be great.
Now my code does a few basic things and would love help on how to make it a concurrent server. Thanks, much appreciated.

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

public class Second {
public static void main(String args[]) throws Exception {

System.setProperty( "java.net.preferIPv4Stack", "true" );

if(args.length != 1)
{
System.err.println("An Error has been found, please enter a port number");
System.exit(1);
}
int port = Integer.parseInt(args[0]);
ServerSocket serverSock=new ServerSocket();


while(true) {
Socket conn = serverSock.accept();
Scanner scanin = new Scanner(conn.getInputStream());
String line=null;
int lineCount=0;

String [] ArrayA = new String [32];

while (true) {
line = scanin.nextLine();
if(line.length()==0) break;

ArrayA [lineCount] = line;
lineCount = lineCount + 1;

}
for(int i=0; i<lineCount; i++) {

System.out.println("line "+ i +": "+ArrayA[i]);
}


Scanner scan = new Scanner (ArrayA[0]);
String command = scan.next();
String resource = scan.next();
String resfile = "http" + resource;


System.out.println("Command" + command);
System.out.println("Resource" + resource);
System.out.println(resfile);


File fname = new File (resfile);
if ( ! fname.exists() ) {
System.out.println("file " + resfile + "File does not exist");


String reply="HTTP/1.0 404 Not Found\r\n" +
"Connection: close\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<h1>Work in progress</h1>\r\n";
OutputStream outs = conn.getOutputStream();
outs.write(reply.getBytes());


} else {
String sizestr = "bytes in file: " + fname.length();
System.out.println(sizestr);

InputStream in = new FileInputStream(fname);
byte pagebuf[] = new byte[100];
while (true)
{
int rc = in.read(pagebuf,0,100);
if( rc<=0 ) break;

String reply="HTTP/1.0 200 OK\r\n" +
"Connection: close\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: " + fname.length() + "\r\n" +
"\r\n";

OutputStream outs=conn.getOutputStream();
outs.write(pagebuf,0,rc);


}
}

conn.close();
}
}
}


 
Vijitha Kumara
Bartender
Posts: 3992
27
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Yaseen Omer, Welcome to CodeRanch

Please UseCodeTags when posting code in the forums. And you may start your own thread for your questions, people get confused answering questions from both of you in the same thread
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!