例子是學習編程的法寶
實現Client端功能的ClientApp
import
import java
import java
public class ClientApp
{
public static void main(String args[])
{
try
{
//創建通訊並且和主機Rock連接
Socket cSocket=new Socket(
//打開這個Socket的輸入/輸出流
OutputStream os=cSocket
DataInputStream is=new DataInputStream(cSocket
int c;
boolean flag=true;
String responseline;
while(flag)
{
//從標准輸入輸出接受字符並且寫如系統
while((c=System
{
os
if(c==
{
os
//將程序阻塞
responseline=is
System
}
}
}
os
is
cSocket
}
catch(Exception e)
{
System
}
}
}
實現Server端功能的ServerApp
import
import java
public class ServerApp
{
public static void main(String args[])
{
try
{
boolean flag=true;
Socket clientSocket=null;
String inputLine;
int c;
ServerSocket sSocket=new ServerSocket(
System
while(flag)
{
clientSocket=sSocket
DataInputStream is= new DataInputStream(new BufferedInputStream(clientSocket
OutputStream os=clientSocket
while((inputLine=is
{
//當客戶端輸入stop的時候服務器程序運行終止!
if(inputLine
{
flag=false;
break;
}
else
{
System
while((c=System
{
os
if(c==
{
os
break;
}
}
}
}
is
os
clientSocket
}
sSocket
}
catch(Exception e)
{
System
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25948.html