?? socketclient.java
字號:
//文件名:SocketClient.java
import java.io.*;
import java.net.*;
class SocketThreadClient extends Thread
{
public static int count = 0;
//構造器,實現服務
public SocketThreadClient (InetAddress addr)
{
count++;
BufferedReader in = null;
PrintWriter out = null;
Socket sk = null;
try{
//使用8000端口
sk = new Socket (addr, 8000);
InputStreamReader isr;
isr = new InputStreamReader (sk.getInputStream ());
in = new BufferedReader (isr);
//建立輸出
out = new PrintWriter (
new BufferedWriter(
new OutputStreamWriter(
sk.getOutputStream ())), true);
//向服務器發送請求
System.out.println("count:"+count);
out.println ("Hello");
System.out.println (in.readLine ());
out.println ("BYE");
System.out.println (in.readLine ());
}
catch (IOException e)
{
System.out.println (e.toString ());
}
finally
{
out.println("END");
//釋放資源
try
{
if (in != null)
in.close ();
if (out != null)
out.close ();
if (sk != null)
sk.close ();
}
catch (IOException e)
{
}
}
}
}
//客戶端
public class SocketClient{
public static void main(String[] args) throws IOException,InterruptedException
{
InetAddress addr = InetAddress.getByName(null);
for(int i=0;i<10;i++)
new SocketThreadClient(addr);
Thread.currentThread().sleep(1000);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -