¨Æ¹ê¤Wºô¸ô½sµ{²³æªº²z¸Ñ´N¬O¨â¥x¹q¸£¬Û¤¬³q°T¼Æ¾Ú¦Ó¤w¡C¹ï©óµ{¦¡û¦Ó¨¥¡A¥h´x´¤¤@ºØ½sµ{¤¶±¨Ã¨Ï¥Î¤@ºØ½sµ{¼Ò«¬¬Û¹ï´N·|Åã±o²³æªº¦h¤F¡CJava SDK´£¨Ñ¤@¨Ç¬Û¹ï²³æªºApi¨Ó§¹¦¨³o¨Ç¤u§@¡ASocket´N¬O¨ä¤¤¤§¤@¡C
¹ï©óJava¦Ó¨¥¡C³o¨ÇApi¦s¦b»PJava.net³oÓ¥]ùر¡C¦]¦¹¥un¾É¤J³oÓ¥]´N¥i¥H·Ç³Æºô¸ô½sµ{¤F¡Cºô¸ô½sµ{ªº°ò¥»¼Ò«¬´N¬O«È¤á¾÷¨ì¦øªA¾¹¼Ò«¬¡C²³æªº»¡´N¬O¨âÓ¶iµ{¤§¶¡¬Û¤¬³q°T¡AµM«á¨ä¤¤¤@Ó¥²¶·´£¨Ñ¤@Ó©T©wªº¦ì¸m¡A¦Ó¥t¤@Ó«h¥u»Ýnª¾¹D³oÓ©T©wªº¦ì¸m¡A¨Ã¥h«Ø¥ß¨âªÌ¤§¶¡ªºÁpô¡CµM«á§¹¦¨¼Æ¾Úªº³q°T´N¥i¥H¤F¡C³o¸Ì´£¨Ñ©T©w¦ì¸mªº³q±`ºÙ¬°¦øªA¾¹¡A¦Ó«Ø¥ßÁpôªº³q±`¥s°µ«È¤áºÝ¡C°ò©ó³oÓ²³æªº¼Ò«¬¡A´N¥i¥H¶i¤Jºô¸ô½sµ{¡C
Java¹ï³oÓ¼Ò«¬ªº¤ä´©¦³«Ü¦hºØApi.¦Ó³o¸Ì§Ú¥u·Q¤¶²Ð¦³ÃöSocketªº½sµ{¤¶±¡C¹ï©óJava¦Ó¨¥¤w¸g²¤Æ¤FSocketªº½sµ{¤¶±¡Cº¥ý§Ų́ӰQ½×¦³Ãö´£¨Ñ©T©w¦ì¸mªºªA°È¤è¬O¦p¦ó«Ø¥ßªº¡CJava´£¨Ñ¤FServerSocket¨Ó¹ï¨ä¶i¦æ¤ä´©¡C¨Æ¹ê¤W·í§A³Ð«Ø¸ÓÃþªº¤@Ó¹ê¤O¹ï¶H¨Ã´£¨Ñ¤@Ӻݤf¸ê·½§A´N«Ø¥ß¤F¤@Ó©T©w¦ì¸m¥i¥HÅý¨ä¥L¹q¸£¨Ó³X°Ý§A¡C
ServerSocket server=new ServerSocket¡]6789¡^¡F
³o¸Ìµy·Lnª`·Nªº¬OºÝ¤fªº¤À°t¥²¶·¬O°ß¤@ªº¡C¦]¬°ºÝ¤f¬O¬°¤F°ß¤@¼ÐÃѨC¥x¹q¸£°ß¤@ªA°Èªº¡C¥t¥~ºÝ¤f¸¹¬O±q0~65535¤§¶¡ªº¡A«e1024Ӻݤf¤w¸g³QTcp/Ip §@¬°«O¯dºÝ¤f¡A¦]¦¹§A©Ò¤À°tªººÝ¤f¥u¯à¬O1024Ó¤§«áªº¡C
¦n¤F¡C§Ú̦³¤F©T©w¦ì¸m¡C²{¦b©Ò»Ýnªº´N¬O¤@®Ú³s±µ½u¤F¡C¸Ó³s±µ½u¥Ñ«È¤á¤èº¥ý´£¥Xn¨D¡C¦]¦¹Java¦P¼Ë´£¨Ñ¤F¤@ÓSocket¹ï¶H¨Ó¹ï¨ä¶i¦æ¤ä´©¡C¥un«È¤á¤è³Ð«Ø¤@ÓSocketªº¹ê¨Ò¹ï¶H¶i¦æ¤ä´©´N¥i¥H¤F¡C
Socket client=new Socket¡]InetAddress.getLocalHost¡]¡^¡A5678¡^¡F
«È¤á¾÷¥²¶·ª¾¹D¦³Ãö¦øªA¾¹ªºIP¦a§}¡C¹ï©óµÛ¤@ÂIJava¤]´£¨Ñ¤F¤@Ó¬ÛÃöªºÃþInetAddress ¸Ó¹ï¶Hªº¹ê¨Ò¥²¶·³q¹L¥¦ªºÀRºA¤èªk¨Ó´£¨Ñ¡C¥¦ªºÀRºA¤èªk¥Dn´£¨Ñ¤F±o¨ì¥»¾÷IP ©M³q¹L¦W¦r©ÎIPª½±µ±o¨ìInetAddressªº¤èªk¡C
¦n¤F¡A¤W±ªº¤èªk°ò¥»¥i¥H«Ø¥ß¤@±ø³s½uÅý¨â¥x¹q¸£¬Û¤¬¥æ¬y¤F¡C¥i¬O¼Æ¾Ú¬O¦p¦ó¶Ç¿éªº©O¡H¨Æ¹ê¤WI/O¾Þ§@Á`¬O©Mºô¸ô½sµ{®§®§¬ÛÃöªº¡C¦]¬°©³¼hªººô¸ô¬OÄ~Äò¼Æ¾Úªº¡C°£«D»·µ{½Õ¥Î¡A³B²z°ÝÃDªº®Ö¤ß¦b°õ¦æ¤W¡C
§_«h¼Æ¾Úªº¥æ¤¬ÁÙ¬O¨Ì¿à©óIO¾Þ§@ªº¡C©Ò¥H§A¤]¥²¶·¾É¤JJava.io³oÓ¥]¡CJavaªºIO¾Þ§@¤]¤£½ÆÂø¡C¥¦´£¨Ñ¤F°w¹ï©ó¦r¸`¬y©MUnicodeªºÅªªÌ©M¼gªÌ¡AµM«á¤]´£¨Ñ¤F¤@Ó½w½Ä¥Î©ó¼Æ¾ÚªºÅª¼g¡C
BufferedReader in=new BufferedReader¡]new InputStreamReader¡]server.getInputStream¡]¡^¡^¡^¡F
PrintWriter out=new PrintWriter¡]server.getOutputStream¡]¡^¡^¡F
¤W±¨â¥y´N¬O«Ø¥ß½w½Ä¨Ã§âì©lªº¦r¸`¬yÂàÅܬ°Unicode¥i¥H¾Þ§@¡C¦Óì©lªº¦r¸`¬y¨Ó·½©óSocketªº¨âÓ¤èªk¡AgetInputStream¡]¡^©MgetOutputStream¡]¡^¤è¡A¤À§O¥Î¨Ó±o¨ì¿é¤J©M¿é¥X¡C¨º»ò²{¦b¦³¤F°ò¥»ªº¼Ò«¬©M°ò¥»ªº¾Þ§@¤u¨ã¡A§ÚÌ¥i¥H°µ¤@Ó²³æªºSocket¨Òµ{¤FªA°È¤è¡G
import Java¡Cio¡C*;
import Java¡Cnet¡C*;
public class MyServer
{
public static void main
(String[] args) throws IOException
{
ServerSocket server=new
ServerSocket(5678);
Socket client=server.accept();
BufferedReader
in=new BufferedReader(new
InputStreamReader
(client.getInputStream()));
PrintWriter out=new
PrintWriter(client.getOutputStream());
while(true)
{
String str=in.readLine();
System.out.println(str);
out.println("has receive...");
out.flush();
if(str.equals("end"))
break;
}
client¡Cclose();
}
}
³oÓµ{¦¡ªº¥Dn¥Øªº¦b©ó¦øªA¾¹¤£Â_±µ¦¬«È¤á¾÷©Ò¼g¤Jªº¸ê°T¥u¨ì¡C«È¤á¾÷µo°e"End"¦r²Å¦ê´N°h¥Xµ{¦¡¡C¨Ã¥B¦øªA¾¹¤]·|°µ¥X"Receive"¬°¦^À³¡C§iª¾«È¤á¾÷¤w±µ¦¬¨ì®ø®§¡C«È¤á¾÷¥N½X¡G
import Java.net.*;
import Java.io.*;
public class Client{
static Socket server;
public static void
main(String[] args)
throws Exception
{
server=new Socket
(InetAddress.getLocalHost()¡A5678);
BufferedReader in=new BufferedReader(new
InputStreamReader(server¡CgetInputStream()));
PrintWriter out=new
PrintWriter(server.getOutputStream());
BufferedReader wt=new BufferedReader(new
InputStreamReader(System.in));
while(true)
{
String str=wt.readLine();
out.println(str);
out.flush();
if(str.equals("end"))
{
break;
}
System.out.println(in.readLine());
}
server.close();
}
}
http://big5.ccidnet.com:89/gate/ ... 1112/1272239_1.html