You are on page 1of 3

Mt chng trnh Socket bng java

09/09/2008 15:52 | 1,154 lt xem

Chng trnh c m t nh sau: 1/ Server: - Lng nghe v chp nhn kt ni t cng 9999. - Cho php nhiu client kt ni n cng mt lc. - Khi client gi n 1 chui th : + Nu chui l "quit" th ngt kt ni vi client. + Tin hnh o chui. + Gi chui c o cho client. 2/ Client: - Kt ni ti Server qua cng 9999. - Nhp chui t bn phm. - Gi chui ti server. - Hin th chui t server gi ti. By gi l cc bc lp trnh: Cc th vin cn thit: import java.io.*; import java.net.*; 1/ Lp trnh pha my ch: server.java: Lp trnh socket th trc ht cn to ra mt i tng socket. V l pha server nn phi l ServerSocket. ServerSocket sk=new ServerSocket(9999); // lng nghe cng 9999 cho php cng mt lc c nhiu client kt ni n th cn c mt tin trnh. Mi khi c mt kt ni th tng ng mt tin trnh c to ra. boolean listening=true; while(listening){ new threadServer(sk.accept()).start(); } Tin trnh c to ra y c tn l thread Server. Lp threadServer c phng thc khi to c tham s l mt i tng socket. socket ny chnh l socket c server chp nhn kt ni. D i y l m ngun ca lp threadServer: import java.io.*; import java.net.*; import java.util.*; public class threadServer extends Thread { Socket socket=null; public threadServer(Socket socket){ this.socket=socket; } public void run(){ try{ PrintWriter out=new PrintWriter(socket.getOutputStream(),true);

BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); String inLine; while(true){ inLine=in.readLine(); Calendar cal =new GregorianCalendar(); if(inLine.equalsIgnoreCase("quit"))break; else { out.println(daochuoi(inLine)); } } }catch(IOException e){ e.printStackTrace(); } } public String daochuoi(String st){ int j=0; Char[] c=st.toCharArray(); for(int i=st.length();i>=0;i--){ c[j]=st.charAt(i); ++j; } String st2= String(c); return st2; } } Nh vy l xong pha my ch. 2/ Lp trnh pha my khch: Class client.java: import java.io.*; import java.net.*; public class client { public static void main(String[] args)throws Exception{ Socket cSk=null; PrintWriter out=null; BufferedReader in=null; try{ cSk=new Socket("127.0.0.1",9999);// khi to mt socket // out : a chui ln server out=new PrintWriter(cSk.getOutputStream(),true); // in: nhn chui c gi t server in=new BufferedReader(new InputStreamReader(cSk.getInputStream())); }catch(IOException e){ e.printStackTrace(); } String inLine; // uIn: cho php nhp mt chui t bn phm.

BufferedReader uIn=new BufferedReader(new InputStreamReader(System.in)); while(true){ inLine=uIn.readLine();// tin hnh c t bn phm v gn chui c c cho inLine if(inLine.equalsIgnoreCase("quit")){ break; } out.println(inLine);// gi chui ln server out.flush(); System.out.println(in.readLine());// in ra mn hnh chui nhn v t server } } } ** Chy chng trnh: - chy file server.java trc sau l client.java. Bn c th dng telnet thay cho client cng c. dng telnet: vo DOS nh dng lnh telnet 127.0.0.1 9999. By gi nhp vo mt chui s nhn c ngay mt chui mi l o ca chui va nhp vo. Trn l chng trnh rt nh. Vi socket bn c th vit hn mt ng dng c tnh thc t nh: chng trnh chat peer - to - peer, hoc mt chng trnh gi th n gin s dng SMTP.