java multithread

java multithread

sir i am send it again sir.please reply me sir.Develop multi-threaded echo server and a corresponding GUI client in java.
View Answers

September 25, 2010 at 9:15 PM

Multi-threaded ECHO SERVER and GUI client program by Whitman Kumaresh for my sister Dhana. .

Any queries mail -> [email protected]



Server program:


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

public class EchoServer {

ServerSocket m_ServerSocket;

public EchoServer() {
try {
m_ServerSocket = new ServerSocket(12111);
} catch (IOException ioe) {
System.out.println("could not create server socket at 12111 Quitting");
System.exit(-1);
}
System.out.println("Listening for clients on 12111....");
int id = 0;
while (true) {
try {
Socket clientSocket = m_ServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
} catch (IOException ioe) {
System.out.println("Exception encountered on accept.Ignoring.StackTrace :");
ioe.printStackTrace();
}
}
}

public static void main(String args[]) {
new EchoServer();
}

class ClientServiceThread extends Thread {

Socket m_clientSocket;
int m_clientID = -1;
boolean m_bRunThread = true;

ClientServiceThread(Socket s, int clientID) {
m_clientSocket = s;
m_clientID = clientID;
}

public void run() {
BufferedReader in = null;
PrintWriter out = null;
System.out.println("Accepted Client :ID - " + m_clientID + " :Address - " + m_clientSocket.getInetAddress().getHostName());
try {
in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream()));
while (m_bRunThread) {
String clientCommand = in.readLine();
System.out.println("Client says :" + clientCommand);
if (clientCommand.equalsIgnoreCase("quit")) {
m_bRunThread = false;
System.out.println("Stopping client thread for client :" + m_clientID);
System.exit(0);
} else {
out.println(clientCommand);
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
m_clientSocket.close();
System.out.println("...STOPPED");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
}



Client Program:



import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class client extends JFrame {

JTextArea clientText;
JTextField msg;
JPanel clip;
JScrollPane clientScroll;
JButton closeButton = new JButton("Close");
JButton send = new JButton("Send");
JLabel label1 = new JLabel("Echo Client - Multithreaded");
Container cont;

client() {
cont = getContentPane();
setSize(250, 500);
setTitle("GUI Client");
clip = new JPanel();
msg = new JTextField(20);
clip.setLayout(new FlowLayout(FlowLayout.CENTER));
clientText = new JTextArea(20, 20);
clientScroll = new JScrollPane(clientText);
clip.add(label1);
clip.add(clientText);
clip.add(msg);
clip.add(send);
clip.add(closeButton);

cont.add(clip);

send.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
Socket s = null;
try {
s = new Socket("localhost", 12111);
} catch (UnknownHostException uhe) {
clientText.setText("UnknownHost: + localhost");
s = null;
} catch (IOException ioe) {
clientText.setText("Cant connect to server at 12111.Make sure it is running");
s = null;
}
if (s == null) {
System.exit(-1);
}
BufferedReader in = null;
PrintWriter out = null;
try {

in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
out.println(msg.getText());
out.flush();
String temp = clientText.getText();
if (temp.equalsIgnoreCase("quit")) {
System.exit(0);
}
msg.setText("");
clientText.append("\nServer says:" + in.readLine());
} catch (IOException ioe) {
clientText.setText("Exception during communication.Server probably closed connection.");
} finally {
try {
out.close();
in.close();
s.close();
} catch (Exception es) {
es.printStackTrace();
}
}
}
});

closeButton.addActionListener(
new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}

public static void main(String args[]) {
client clientFrame = new client();
clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clientFrame.setVisible(true);
}
}


Enjoy..!!









Related Tutorials/Questions & Answers:
Java Multithread
Java Multithread  Hi All. I am trying to make a program in Java that uses Multithread. That program is about a bank account shared between husband and wife who use debit cards. I wrote the Java program using 2 threads but i am
Java MultiThread - Java Beginners
Java MultiThread  what is the meaning of standalone application in multithread.How can we write multi programms in multithread process and execution...."); } } ----------------------------------------------------- Visit for more information. http://www.roseindia.net/java/thread/Java
Advertisements
java multithread - Java Beginners
java multithread  Hi, Thanks for your multithreading java code. It helped me a lot. But i have one issue. My code is look like this. public class classname extends Thread { public classname() { super
java multithread - Java Beginners
java multithread  Hi, I have developed one java application.It should be running continuously. Using multithreads how to make the application to run continuously. Could you please help me. Thank you,  Hi Friend
java multithread - Development process
java multithread  sir i am send it again sir.please reply me sir.Develop multi-threaded echo server and a corresponding GUI client in java.  Multi-threaded ECHO SERVER and GUI client program by Whitman Kumaresh for my
Java MultiThread
Java has multithreading feature. Multithreading feature is given by Thread class and Runnable interface Java Multithreading allows to run multiple tasks(threads) at a time. Java Multi Thread Example public class multi
ModuleNotFoundError: No module named 'multithread'
ModuleNotFoundError: No module named 'multithread'  Hi, My Python... 'multithread' How to remove the ModuleNotFoundError: No module named 'multithread' error? Thanks   Hi, In your python environment
Java Interview Questions - Page 5
Java Interview Questions - Page 5       Question: How to create multithread in a program... to start the thread. Question: Can Java object be locked down for exclusive
Multithreading ? - Java Interview Questions
Multithreading ?   Hi Friends, I am new to java , am not clear with Multithreading. Multithread will cause deadlock. We can... program. For read more information on Multithread visit to : http
Java Exception Thread
Java Exception Thread     ... the program. Many Thread run concurrently in the program. Multithread are those...,the thread run concurently,synchronous or asynchronous. Advantage of Multithread
Java Thread In Applet
Java Thread Applet is a java class that runs inside the internet browser. It is used to make the gui application, network application in java Thread is used in applet to make the multithread application Example of Java Thread
Java Exception - Handle Exceptions in Java
Java Exception - Handle Exceptions in Java       What is Exception in Java? How to handle the Exceptions in Java programming language. This section on exception
Developing Distributed application using Enterprise Java Beans, J2EE Architecture, EJB Tutorial, WebLogic Tutorial.
architecture. It also eliminates the need of writing thin-client multithread.... ADS_TO_REPLACE_3 In the Java TM 2 Platform...: Java Database Connectivity (JDBC)  Java
Developing Distributed application using Enterprise Java Beans, J2EE Architecture, EJB Tutorial, WebLogic Tutorial.
Java Get Example
Java Get Example       In this section of Java Examples, we are going to illustrate various Java..... If you are a Java beginner this is a best place for you to learn different
java
java  diff bt core java and java
java
java  what is java
JAVA
JAVA  how the name came for java language as "JAVA
java
java   why iterator in java if we for loop
java
java  explain technologies are used in java now days and structure java
java
java  different between java & core java
Java
Java   Whether Java is pure object oriented Language
java
java  is java open source
java
java  what is java reflection
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java   What is ?static? keyword
java
java  RARP implementation using java socket
java
java  sample code for RARP using java
java
java  Does java allows multiline comments
Java
Java  how to do java in command prompt
java
java  Write a java code to print "ABABBABCABABBA
java
java  write a program in java to acess the email
java
java  send me java interview questions
java
java  how use java method
java
java  what are JAVA applications development tools
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  is java purely object oriented language
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java   difference between class and interface
Java
Java  how to draw class diagrams in java
java
java  write a java program using filenotfoundexception
java
java  hi im new to java plz suggest me how to master java[email protected]

Ads