.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

SERVER SIDE PROGRAM USING THREAD

Posted By:iswarya       Posted Date: October 18, 2013    Points: 40    Category: Javascript    URL: http://www.dotnetspark.com  

SERVER CHAT PROGRAMUSING THREAD
 

import java.io.*;

import java.net.*;

 

class Server

{

static ServerSocket ser=null;

static final int port=8888;

static Socket soc=null;

public static void main(String a[])  throws IOException

{

try

{

ser=new ServerSocket(port);

System.out.println("server port:"+port);

soc=ser.accept();

System.out.println("server connected ........"+soc);

new write(soc);

new read(soc);

}

catch(Exception e)

{}

}

}

 

class write implements Runnable

{

Socket soc;

Thread t=null;

write(Socket soc)

{

this.soc=soc;

t=new Thread(this);

t.start();

}

public void run()

{

while(true)

{

try

{

DataInputStream ds=new DataInputStream(System.in);

String s=ds.readLine();

//System.out.println(s);

DataOutputStream dos=new DataOutputStream(soc.getOutputStream());

dos.writeUTF(s);

}

catch(Exception e)

{

}

}}}

 

class read  implements Runnable

{

Socket soc;

Thread t=null;

read(Socket soc)

{

this.soc=soc;

t=new Thread(this);

t.start();

}

public void run()

{

while(true)

{

try

{

DataInputStream ds=new DataInputStream(soc.getInputStream());

String s=ds.readUTF();

System.out.println(s);

}

catch(Exception e)

{

}

}}}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend