0% found this document useful (0 votes)
39 views

Server Program

The document contains code for several Java programs including one that looks for local ports that are in use, a daytime server that returns the current date/time, a client tester that connects to servers, and an HTTP server that serves the same file content in chunks. It defines classes for input/output threads to handle client connections and reads/writes data from both the client and a file to send over the network.

Uploaded by

Aswaraj Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Server Program

The document contains code for several Java programs including one that looks for local ports that are in use, a daytime server that returns the current date/time, a client tester that connects to servers, and an HTTP server that serves the same file content in chunks. It defines classes for input/output threads to handle client connections and reads/writes data from both the client and a file to send over the network.

Uploaded by

Aswaraj Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

//Look for local ports

import java.net.*;
import java.io.*;
public class lookForLocalPorts {
public static void main(String[] args {

ServerSocket t!eServer;
"or (int i # $%&'; i (# )**+*; i,, {
tr- {
.. t!e ne/t line 0ill "ail and drop into t!e catc! block i"
.. t!ere is alread- a server running on port i
t!eServer # ne0 ServerSocket(i;
t!eServer.close(;
1
catc! (234/ception e {
S-stem.out.println(56!ere is a server on port 5 , i , 5.5;
1 .. end tr-
1 .. end "or
1

1
/////////////////////////////////////
A day time server
import java.net.*;
import java.io.*;
import java.util.7ate;

public class da-timeServer {

public "inal static int da-timePort # $+;
public static void main(String[] args {
ServerSocket t!eServer;
Socket t!e8onnection;
PrintStream p;
tr- {
t!eServer # ne0 ServerSocket(da-timePort;
tr- {
0!ile (true {
t!e8onnection # t!eServer.accept(;
p # ne0 PrintStream(t!e8onnection.get3utputStream(;
p.println(ne0 7ate(;
t!e8onnection.close(;
1
1
catc! (234/ception e {
t!eServer.close(;
S-stem.err.println(e;
1
1 .. end tr-
catc! (234/ception e {
S-stem.err.println(e;
1
1
1
//////////////////////////////
a client tester
import java.net.*;
import java.io.*;
public class client6ester {
public static void main(String[] args {
int t!ePort;
ServerSocket ss;
Socket t!e8onnection;

tr- {
t!ePort # 2nteger.parse2nt(args[%];
1
catc! (4/ception e {
t!ePort # %;
1

tr- {
ss # ne0 ServerSocket(t!ePort;
S-stem.out.println(5Listening "or connections on port 5 ,
ss.getLocalPort(;
0!ile (true {
t!e8onnection # ss.accept(;
S-stem.out.println(58onnection establis!ed 0it! 5 ,
t!e8onnection;
2nput6!read it # ne0
2nput6!read(t!e8onnection.get2nputStream(;
it.start(;
3utput6!read ot # ne0
3utput6!read(t!e8onnection.get3utputStream(9 it;
ot.start(;
.. need to 0ait "or ot and it to "inis!
tr- {
ot.join(;
it.join(;
1
catc! (2nterrupted4/ception e {
1
1
1
catc! (234/ception e {

1

1
1
class 2nput6!read e/tends 6!read {

2nputStream is;

public 2nput6!read(2nputStream is {
t!is.is # is;
1
public void run( {

tr- {
0!ile (true {
int i # is.read(;
i" (i ## :$ break;
c!ar c # (c!ar i;
S-stem.out.print(c;
1
1
catc! (234/ception e {
S-stem.err.println(e;
1

1
1
class 3utput6!read e/tends 6!read {

PrintStream ps;
7ata2nputStream is;
2nput6!read it;

public 3utput6!read(3utputStream os9 2nput6!read it {
ps # ne0 PrintStream(os;
t!is.it # it;
is # ne0 7ata2nputStream(S-stem.in;
1
public void run( {
String line;
tr- {
0!ile (true {
line # is.readLine(;
i" (line.e;uals(5.5 break;
ps.println(line;
1
1
catc! (234/ception e {

1
it.stop(;
1
1
///////////////////////////////////////////
HTTP server that chunks same file
import java.net.*;
import java.io.*;
import java.util.*;
public class one"ile e/tends 6!read {
static String t!e7ata # 55;
static String 8ontent6-pe;
Socket t!e8onnection;
public static void main(String[] args {
int t!ePort;
ServerSocket ss;
Socket t!e8onnection;
File2nputStream t!eFile;
.. cac!e t!e "ile
tr- {
t!eFile # ne0 File2nputStream(args[%];
7ata2nputStream dis # ne0 7ata2nputStream(t!eFile;
i" (args[%].ends<it!(5.!tml5 == args[%].ends<it!(5.!tm5 {
8ontent6-pe # 5te/t.!tml5;
1
else {
8ontent6-pe # 5te/t.plain5;
1

tr- {
String t!isLine # 55;
0!ile ((t!isLine # dis.readLine( ># null {
t!e7ata ,# t!isLine , 5?n5;
1
1
catc! (4/ception e {
S-stem.err.println(54rror@ 5 , e;
1
1
catc! (4/ception e {
S-stem.err.println(e;
S-stem.err.println(5Asage@ java one"ile "ilename port5;
S-stem.e/it($;
1

.. set t!e port to listen on
tr- {
t!ePort # 2nteger.parse2nt(args[$];
i" (t!ePort ( % == t!ePort B )**+* t!ePort # C%;
1
catc! (4/ception e {
t!ePort # C%;
1

tr- {
ss # ne0 ServerSocket(t!ePort;
S-stem.out.println(5Dccepting connections on port 5
, ss.getLocalPort(;
S-stem.out.println(57ata to be sent@5;
S-stem.out.println(t!e7ata;
0!ile (true {
one"ile "s # ne0 one"ile(ss.accept(;
"s.start(;
1
1
catc! (234/ception e {

1

1

public one"ile(Socket s {
t!e8onnection # s;
1
public void run( {

tr- {
PrintStream os # ne0 PrintStream(t!e8onnection.get3utputStream(;
7ata2nputStream is # ne0
7ata2nputStream(t!e8onnection.get2nputStream(;
String re;uest # is.readLine(;
.. 2" t!is is E66P.$.% or later send a F2F4 !eader
i" (re;uest.inde/3"(5E66P.5 ># :$ {
0!ile (true { .. read t!e rest o" t!e F2F4 !eader
String t!isLine # is.readLine(;
i" (t!isLine.trim(.e;uals(55 break;
1

os.print(5E66P.$.% &%% 3G?r?n5;
7ate no0 # ne0 7ate(;
os.print(57ate@ 5 , no0 , 5?r?n5;
os.print(5Server@ 3neFile $.%?r?n5;
os.print(58ontent:lengt!@ 5 , t!e7ata.lengt!( , 5?r?n5;
os.print(58ontent:t-pe@ 5 , 8ontent6-pe , 5?r?n?r?n5;
1 .. end i"
os.println(t!e7ata;
t!e8onnection.close(;
1 .. end tr-
catc! (234/ception e {

1
1
1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy