Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

java Socket ͨѶµÄ´úÂëÀý×Ó

java Socket ͨѶµÄ´úÂëÀý×Ó
ʵÏÖClient¶Ë¹¦ÄܵÄClientApp.javaÔ­Îļþ£º
import java.net.*;
import java.io.*;
import java.lang.*; 
public class ClientApp
{
public static void main(String args[])
{
try
{
//´´½¨Í¨Ñ¶²¢ÇÒºÍÖ÷»úRockÁ¬½Ó
Socket cSocket=new Socket("192.168.100.188",8018);
//´ò¿ªÕâ¸öSocketµÄÊäÈë/Êä³öÁ÷
OutputStream os=cSocket.getOutputStream();
DataInputStream is=new DataInputStream(cSocket.getInputStream());
int c;
boolean flag=true;
String responseline;
while(flag)
{
//´Ó±ê×¼ÊäÈëÊä³ö½ÓÊÜ×Ö·û²¢ÇÒдÈçϵͳ
while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush();
//½«³ÌÐò×èÈû£¬Ö±µ½»Ø´ðÐÅÏ¢±»ÊÕµ½ºó½«ËûÃÇÔÚ±ê×¼Êä³öÉÏÏÔʾ³öÀ´
responseline=is.readLine();
System.out.println("Message is:"+responseline);
}
}
}
os.close();
is.close();
cSocket.close();
}
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}
ʵÏÖServer¶Ë¹¦ÄܵÄServerApp.javaÔ­Îļþ£º
import java.net.*;
import java.io.*;
public class ServerApp
{
public static void main(String args[])
{
try
{
boolean flag=true;
Socket clientSocket=null;
String inputLine;
int c;
ServerSocket sSocket=new ServerSocket(8018);
System.out.println("Server listen on:"+sSocket.getLocalPort());
while(flag)
{
clientSocket=sSocket.accept();
DataInputStream is= new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
OutputStream os=clientSocket.getOutputStream();
while((inputLine=is.readLine())!=null)
{
//µ±¿Í»§¶ËÊäÈëstopµÄʱºò·þÎñÆ÷³ÌÐòÔËÐÐÖÕÖ¹£¡
if(inputLine.equals("stop"))
{
flag=false;
break;
}
else
{
System.out.println(inputLine);
while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c=='\n')
{
os.flush(); //½«ÐÅÏ¢·¢Ë͵½¿Í»§¶Ë
break;
}
}
}
}
is.close();
os.close();
clientSocket.close();
}
sSocket.close();
}
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());


Ïà¹ØÎĵµ£º

Java NIO APIÏê½â

 
Java NIO APIÏê½â
ÔÚJDK
1.4ÒÔÇ°£¬JavaµÄIO²Ù×÷¼¯ÖÐÔÚjava.ioÕâ¸ö°üÖУ¬ÊÇ»ùÓÚÁ÷µÄ×èÈû£¨blocking£©API¡£¶ÔÓÚ´ó¶àÊýÓ¦ÓÃÀ´Ëµ£¬ÕâÑùµÄAPIʹÓúܷ½
±ã£¬È»¶ø£¬Ò»Ð©¶ÔÐÔÄÜÒªÇó½Ï¸ßµÄÓ¦Óã¬ÓÈÆäÊÇ·þÎñ¶ËÓ¦Óã¬ÍùÍùÐèÒªÒ»¸ö¸üΪÓÐЧµÄ·½Ê½À´´¦ÀíIO¡£´ÓJDK 1.4Æð£¬NIO
API×÷Ϊһ¸ö»ùÓÚ»º³åÇø£¬²¢ÄÜÌṩ·Ç×èÈû(non-blo ......

½âÎöJavaÀàºÍ¶ÔÏóµÄ³õʼ»¯¹ý³Ì

ÀàµÄ³õʼ»¯ºÍ¶ÔÏó³õʼ»¯ÊÇ JVM ¹ÜÀíµÄÀàÐÍÉúÃüÖÜÆÚÖзdz£ÖØÒªµÄÁ½¸ö»·½Ú£¬Google ÁËÒ»±éÍøÂ磬ÓйØÀà×°ÔØ»úÖƵÄÎÄÕµ¹ÊDz»ÉÙ£¬È»¶øÀà³õʼ»¯ºÍ¶ÔÏó³õʼ»¯µÄÎÄÕ²¢²»¶à£¬ÌرðÊÇ´Ó×Ö½ÚÂëºÍ JVM ²ã´ÎÀ´·ÖÎöµÄÎÄÕ¸üÊÇÏÊÓÐËù¼û¡£
±¾ÎÄÖ÷Òª¶ÔÀàºÍ¶ÔÏó³õʼ»¯È«¹ý³Ì½øÐзÖÎö£¬Í¨¹ýÒ»¸öʵ¼ÊÎÊÌâÒýÈ룬½«Ô´´úÂëת»»³É JVM ×Ö½ÚÂëºó£¬ ......

javaÖÐequalsºÍ==µÄÇø±ð

ÖµÀàÐÍÊÇ´æ´¢ÔÚÄÚ´æÖеĶÑÕ»£¨ÒÔºó¼ò³ÆÕ»£©£¬¶øÒýÓÃÀàÐ͵ıäÁ¿ÔÚÕ»Öнö½öÊÇ´æ´¢ÒýÓÃÀàÐͱäÁ¿µÄµØÖ·£¬¶øÆä±¾ÉíÔò´æ´¢ÔÚ¶ÑÖС£
==²Ù×÷±È½ÏµÄÊÇÁ½¸ö±äÁ¿µÄÖµÊÇ·ñÏàµÈ£¬¶ÔÓÚÒýÓÃÐͱäÁ¿±íʾµÄÊÇÁ½¸ö±äÁ¿ÔÚ¶ÑÖд洢µÄµØÖ·ÊÇ·ñÏàͬ£¬¼´Õ»ÖеÄÄÚÈÝÊÇ·ñÏàͬ¡£
equals²Ù×÷±íʾµÄÁ½¸ö±äÁ¿ÊÇ·ñÊǶÔͬһ¸ö¶ÔÏóµÄÒýÓ㬼´¶ÑÖеÄÄÚÈÝÊÇ·ñ ......

JavaΪʲôҪ¿ªÔ´£¿

JavaÖÕÓÚ¿ªÔ´ÁË£¬²ÉÓÃGPLv2ÊÚȨЭÒé¡£
Java¿ªÔ´µÄ×îÖÕ·½°¸ºÍʱ¼ä±í£¬ÎÒÊÇÔÚ10Ô³õµÄʱºòÄõ½µÄ¡£´ÓÄǸöʱºòÆðÎÒ¾ÍÉîÉîµÄÌå»áµ½±£ÊØÒ»¸ö×Ô¼º·Ç³£Ï£Íû¹«¿ªµÄÃØÃÜÕæÊÇÒ»¼þ·Ç³£ÄÑÊܵÄÊÂÇ飬ÄÄÅÂʱ¼äÖ»Óж̶̵ÄÒ»¸öÔ¡£Sun ¹«Ë¾µÄ¹Ù·½ÏûÏ¢ÊÇ11ÔÂ13ÈÕ£¨ÃÀ¹úʱ¼ä£©·¢²¼µÄ£¬µ«ÊÇÔÚ11ÔÂ12ÈÕ£¨Ò²¾ÍÊÇÖйúµÄ11ÔÂ13ÈÕ£©µÄʱºò£¬»¥ÁªÍøÉ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ