package socket;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
* 聊天室客户端
* @author uid
*
*/
public class Client1 {
/*
* java.net.Socket 套接字
* Socket 封装了TCP协议传输数据的细节,使得
* 我们可以通过两条流的读写完成与远端计算机的数据交互
*/
private Socket socket;
/*
* 构造方法用来初始化客户端
*/
public Client1(int host){
try {
/*
* 实例化Socket的过程就是连接服务端的
* 过程
* 参数1:服务器的IP地址
* 参数2:服务端程序打开端口
*
* 我们通过IP可以找到网络上的服务端所在计算,通过端口
* 可以找到该计算上运行的服务端应用程序从而建立连接
*/
System.out.println("正在连接服务器...");
socket=new Socket("176.125.111."+host,8088);
System.out.println("已连接服务端!"+host);
//localhost 计算机本身的意思
}catch(Exception e) {
e.printStackTrace();
}
}
//客户端开始工作的方法
public void start() {
try {
Scanner s=new Scanner(System.in);
/*
* Socket提供的方法:
* OutputStream getOutputStream();
* 通过网络发送给远程计算机.
*/
OutputStream out=socket.getOutputStream();
OutputStreamWriter osw =new OutputStreamWriter(out,"utf-8");
BufferedWriter bw=new BufferedWriter(osw);
PrintWriter pw=new PrintWriter(bw,true);
// String line=s.nextLine();
//String line ="cxk";
//System.out.println("请输入信息:");
while(true) {
// pw.println("よ!");
// pw.println("");
}
// String sta=null;
// while(!"exit".equals(sta)) {
// sta=s.nextLine();
// if(!"exit".equals(lien)) {
// pw.println();
// }
// }
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
for (int i = 0; i <256; i++) {
Thread t=new MyThread(i);
t.start();
}
//
// for (int j = 0; j <65000 ; j++) {
// Thread t1=new MyThreads(j);
//
// t1.start();
//
// }
}
}
class MyThread extends Thread {
private int i;
public MyThread (int i) {
this.i=i;
}
public void run() {
Client1 client1 =new Client1(i);
client1.start();
}
}
class MyThreads extends Thread{
private int b;
public MyThreads (int b) {
this.b=b;
}
public void run() {
Client1 client2 =new Client1(b);
client2.start();
}
}
循环端口怎么加入。。个人比较钻牛角尖希望能弄明白