Learn more about IndiaStudyChannel
Install Alexa Toolbar
and earn more...
 
Communities Members BookmarksPolls Fresher Jobs Funny Photos B.Tech Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter




MMulti threaded Socket Server-Client Chat Program - Hello World


Posted Date: 04 Oct 2008    Resource Type: Articles/Knowledge Sharing    Category: Computer & Technology
Author: mhshoiabMember Level: Bronze    
Rating: Points: 2




Hi,

You will find three classes, Server.java, Client.java and CreateThread.java
first run the server and then client, you can open many clients, the server code will support. This example is just a hello world type, so dont expect more. It will just teach you on how to create multithreaded socket. Happy learning.. Write to me for more JAVA...........:). I proclaim that this code is fully mine, since being a corporate trainer, I make Technology easy with these simple examples. Keep looking for more ....


package day4.netpack;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
ServerSocket ss;Socket s;
BufferedReader br,br1;PrintStream ps;
public Server()
{
try{
ss=new ServerSocket(2000);
System.out.println("Server Ready");
while(true){
s=ss.accept();
CreateThread.getInstance().spanThread(s);
}
}catch(Exception

e){System.out.println(e);}
}
public static void main(String[] args) {
new Server();
}
}
.............

package day4.netpack;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Client {
Socket s;PrintStream ps;BufferedReader br,br1;
public Client()
{
try{
s=new Socket("localhost",2000);
while(true){
ps=new PrintStream(s.getOutputStream());
br1=new BufferedReader(new InputStreamReader(System.in));
String input=br1.readLine();
ps.println(input);

br=new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(br.readLine());
}
}catch(Exception e){System.out.println(e);}
}
public static void main(String[] args) {
new Client();
}
}
.................

package day4.netpack;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class CreateThread implements Runnable{
Socket s;Thread t;BufferedReader br,br1;PrintStream ps;
static CreateThread obj;
private CreateThread()
{}
public static CreateThread getInstance(){
if(obj==null){obj=new CreateThread();return obj;}
else{return obj;}
}
public void spanThread(Socket s)
{
this.s=s;
t=new Thread(this);t.start();
}
public void run()
{
try{
while(true){
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(br.readLine());

ps=new PrintStream(s.getOutputStream());
br1=new BufferedReader(new InputStreamReader(System.in));
String input=br1.readLine();
ps.println(input);
}
}catch(Exception e){System.out.println(e);}
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Kaspersky 2009
Previous Resource: File tracing in google desktop
Return to Discussion Resource Index
Post New Resource
Category: Computer & Technology


Post resources and earn money!
 
Related Resources

Watch TV Channels



Contact Us    Editors    Privacy Policy    Terms Of Use   

ISC Technologies. 2006 - 2008 All Rights Reserved.