Program : Client-Server Program using TCP/IP
By : Kapil Lohia
Program 1:
import java.net.*; import java.io.*; class tcpip_server { public static void main(String args[]) throws IOException { ServerSocket n1=null; try { n1=new ServerSocket(98); } catch(IOException e) { System.err.println("Port 98 could not be found"); System.exit(1); } Socket c=null; try { c=n1.accept(); System.out.println("Connection from "+c); } catch(IOException e) { System.out.println("Accept failed"); System.exit(1); } PrintWriter out=new PrintWriter(c.getOutputStream(),true); BufferedReader in=new BufferedReader(new InputStreamReader(c.getInputStream())); String n; BufferedReader sin=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Ready to type now"); while((n=sin.readLine())!=null) { out.println(n); } out.close(); c.close(); n1.close(); } }
Program 2
import java.net.*; import java.io.*; class tcpip_client { public static void main(String args[]) throws IOException { Socket s=null; BufferedReader b=null; try { s=new Socket(InetAddress.getLocalHost(),98); b=new BufferedReader(new InputStreamReader(s.getInputStream())); } catch(UnknownHostException u) { System.err.println("I don't know host"); System.exit(0); } String inp; while((inp=b.readLine())!=null) { System.out.println(inp); } b.close(); s.close(); } }
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
October 27th, 2010 at 12:30 pm
java program for Client-Server Program using TCP/IP
April 22nd, 2011 at 11:59 am
firstly thank you so much for the help ! this is really a wonderful helpful site for the students. I want to stay tuned with ELECTROFRIENDS for lifelong. once again ,thank you so very much!
May 29th, 2011 at 5:27 pm
thanks dear….
July 25th, 2011 at 10:28 am
thnx for the help !!!!!
July 25th, 2011 at 10:31 am
its really helpfull
July 26th, 2011 at 10:48 pm
Very nice post.I was seeking like this post Green Life
August 22nd, 2011 at 7:34 pm
thank u sir
September 7th, 2011 at 8:14 pm
good job.and thanks
January 4th, 2012 at 9:50 am
thanks ……. really it is helpful
January 4th, 2012 at 11:55 am
Hi………. not even single error in ur codes…. thank u very much!!!! REALLY AWESOME!!!!
January 31st, 2012 at 2:01 pm
bakwas… whr to give the ip address and port num.
January 31st, 2012 at 2:03 pm
aa bacho log randi baji karo.. inn sab main kya pada hai..
randi khane main jo maja hai yaha nahi