WELCOME

Java

ATM_ISO8583 (1)

ere i am adding all of my Java coding samples for reference.  This will help me to recall , give me your comments if any wrong code presented, which will help to develop my knowledge in Java .

Servlet Socket Program

TCP Server Sample

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

class servertcp extends Thread

{

public static void main(String argv[]) throws Exception

{

int port = 5000;
ServerSocket Server = new ServerSocket (port);
System.out.println (“TCPServer Waiting for client on port ” + port);
int id = 0;
while(true){
Socket connected = Server.accept();
ClientServiceThread cliThread = new ClientServiceThread(connected, id++);
cliThread.start();
}
}
}

class ClientServiceThread extends Thread {
Socket clientSocket;
int clientID = -1;
boolean running = true;

ClientServiceThread(Socket s, int i) {
clientSocket = s;
clientID = i;
}

public void run()
{
System.out.println (“TCPServer Waiting for client on port 5000″);
String fromclient;
String toclient;
try{
while(true) {
//System.out.println( ” THE CLIENT”+” “+connected.getInetAddress() +”:”+connected.getPort()+” IS CONNECTED “);
System.out.println(“Connected with client ‘IPAdress’ :” +clientSocket.getInetAddress());
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
BufferedReader inFromClient = new BufferedReader(new InputStreamReader (clientSocket.getInputStream()));
PrintWriter outToClient = new    PrintWriter(clientSocket.getOutputStream(),true);
String temp = “”;
while (true) {
System.out.println(“Press ‘X’ to Quit or Press ‘Enter’ to send the file:”);
toclient = inFromUser.readLine();
FileInputStream fstream = new FileInputStream(“C://TCPIP/sample.txt”);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)   {
temp =temp + strLine +”|”;
}
in.close();
if ( toclient.equalsIgnoreCase(“x”)) {
outToClient.println(toclient);
clientSocket.close();
break;
} else {
outToClient.println(temp);
System.out.println(“File Send to the Client”);
}
fromclient = inFromClient.readLine();
if ( fromclient.equalsIgnoreCase(“x”)) {
clientSocket.close();
break;
}else {
System.out.println( “RECIEVED:” + fromclient );
}
}
}
}catch(Exception e)
{
System.out.println(“Error in MultiThreading”);
}
}
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: