Beruflich Dokumente
Kultur Dokumente
CRC Program: -
import java.util.*;
class CRC {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n;
//A Webserver waits for clients to connect, then starts a seperate thread to handle the request.
String s=in.readline();
System.out.println(s); //Log the request
String filename="";
String Tokenizer st=new StringTokenizer(s);
try
{
//Parse the filename from the GET command
if(st.hasMoreElements() && st.nextToken().equalsIgnoreCase("GET")
&& st.hasMoreElements())
filename=st.nextToken();
else
throw new FileNotFoundException();
//Bad request
//Append trailing "/" with "index.html"
if(filename.endsWith("/"))
filename+="index.html";
while (filename.indexOf("/")==0)
filename=filename.substring(1);
filename=filename.replace('/', File.seperator.charAt(0));
if(filename.indexOf("..")>=0 || filename.indexOf(':')>=0 ||
filename.indexOf('|')>=0)
throw new FileNotFoundException();
if(new File(filename).isDirectory())
{
filename=filename.replace('\\', '/');
out.print("HTTP/1.0 301 Moved Permanently\r\n"+"Location:
/"+filename+"/\r\n\r\n");
out.close();
return;
}
String mimeType="text/plain";
if(filename.endswith(".html") || filename.endswith(".htm"))
mimeType="text/html";
else if(filename.endswith(".jpg") || filename.endswith(".jpeg"))
mimeType="image/jpeg";
else if(filename.endswith(".gif"))
mimeType="image/gif";
else if(filename.endswith(".class"))
mimeType="application/octet-stream";
out.print("HTTP/1.0 200 OK\r\n"+"Content-type:
"mimeType+"\r\n\r\n");
catch (FileNotFoundException x)
{
out.println("HTTP/1.0 404 Not Found\r\n"+"Content-type:
text/html\r\n\r\n"+"<html><head></head><body>"+filename+"not found</body></html>\n");
out.close;
}
}
catch (IOException x)
{
System.out.println(x);
}
}
}
3. TCP Client
import java.io.*;
import java.net.*;
class client{
while(!(sentence.equals(""))){
outToServer.writeBytes(sentence+"\n");
modifiedSentence = inFromServer.readLine();
System.out.println(modifiedSentence);
System.out.println("enter ur msg");
sentence = inFromUser.readLine();}
clientSocket.close();}
}
4. TCP Server
import java.io.*;
import java.net.*;
import java.lang.*;
class server{
public static void main(String args[]) throws Exception{
String clientSentence;
String capitalizedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true){
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
try{
while(!(clientSentence.equals(""))){
System.out.println("from client:"+clientSentence);
String UserReply = inFromUser.readLine();
capitalizedSentence = "reply from server"+ UserReply +"\n";
outToClient.writeBytes(capitalizedSentence);
clientSentence = inFromClient.readLine();}
}
catch(Exception e){
System.out.println("chat ended");
welcomeSocket.close();
}}}}
class server{
public static void main(String args[]) throws Exception{
String clientSentence;
String capitalizedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true){
}
}
}
}
catch(Exception e){
System.out.println("Exception is found");
}}}
6. UDP Client
import java.net.*;
class MyUDPC
{
public static int serverPort = 998;
public static int clientPort = 999;
public static int buffer_size = 1024;
public static DatagramSocket ds;
public static byte buffer[] = new byte[buffer_size];
7. UDP Server
import java.net.*;
class MyUDPS
{
public static int serverPort = 998;
public static int clientPort = 999;
public static int buffer_size = 1024;
public static DatagramSocket ds;
public static byte buffer[] = new byte[buffer_size];