1.效果是多个客户端和一个服务器,当客户端第一次连接服务器的socket后,客户端的socket一直不断开(我这里没关闭socket,除非手动退出,或出现异常),总是接受服务器传过来的数据
2.现在客户端socket连接服务器的socket后,第一次接受到服务器的数据后,以后就不能接受到数据了,也没有报错,客户端和服务器端都没有退出socket3. 这样是否有性能问题?public class TCPClient {
public static void main(String[] args) throws Exception{
new Thread() {
DataOutputStream dos = null;
DataInputStream br = null;
Socket s = new Socket("192.168.0.9",5566); //创建一个Socket对象,连接IP地址为192.168.24.177的服务器的5566端口
@Override
public void run() {
try {
dos = new DataOutputStream(s.getOutputStream()); //获取Socket对象的输出流,并且在外边包一层DataOutputStream管道,方便输出数据
dos.writeUTF("1"); //DataOutputStream对象的writeUTF()方法可以输出数据,并且支持中文
dos.flush(); //确保所有数据都已经输出
while (true) {
br = new DataInputStream(s.getInputStream());
String str;
if((str = br.readUTF().trim())!=null){
System.out.println("收到:"+str);
}
}
} catch (Exception e) {
System.out.println("Error getclient.run():"+e.getMessage());
}
}
}.start();
// s.close(); //关闭Socket连接
}
}public class TCPServer {
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(5566); //创建一个Socket服务器,监听5566端口
int i=0;
//利用死循环不停的监听端口
try {
while(true){
Socket s = ss.accept();//利用Socket服务器的accept()方法获取客户端Socket对象。
i++;
System.out.println("第" + i +"个客户端成功连接!");
Client c = new Client(i,s); //创建客户端处理线程对象
Thread t =new Thread(c); //创建客户端处理线程
t.start(); //启动线程
}
} catch (Exception e) {
System.out.println("01"+e.getMessage());
}
}
} //客户端处理线程类(实现Runnable接口)
class Client implements Runnable{
int clientIndex = 0; //保存客户端id
Socket s = null; //保存客户端Socket对象
Client(int i,Socket s){
clientIndex = i;
this.s = s;
}
public void run(){
//打印出客户端数据
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = null;
dos = new DataOutputStream(s.getOutputStream()); //获取Socket对象的输出流,并且在外边包一层DataOutputStream管道,方便输出数据
dos.writeUTF("服务器的数据拉"); //DataOutputStream对象的writeUTF()方法可以输出数据,并且支持中文
//dos.flush(); //确保所有数据都已经输出
if (dis.readUTF().equals("1")){
System.out.println("第" + clientIndex + "个客户端发出消息:" + dis.readUTF());
}
//dis.close();
//s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
2.现在客户端socket连接服务器的socket后,第一次接受到服务器的数据后,以后就不能接受到数据了,也没有报错,客户端和服务器端都没有退出socket3. 这样是否有性能问题?public class TCPClient {
public static void main(String[] args) throws Exception{
new Thread() {
DataOutputStream dos = null;
DataInputStream br = null;
Socket s = new Socket("192.168.0.9",5566); //创建一个Socket对象,连接IP地址为192.168.24.177的服务器的5566端口
@Override
public void run() {
try {
dos = new DataOutputStream(s.getOutputStream()); //获取Socket对象的输出流,并且在外边包一层DataOutputStream管道,方便输出数据
dos.writeUTF("1"); //DataOutputStream对象的writeUTF()方法可以输出数据,并且支持中文
dos.flush(); //确保所有数据都已经输出
while (true) {
br = new DataInputStream(s.getInputStream());
String str;
if((str = br.readUTF().trim())!=null){
System.out.println("收到:"+str);
}
}
} catch (Exception e) {
System.out.println("Error getclient.run():"+e.getMessage());
}
}
}.start();
// s.close(); //关闭Socket连接
}
}public class TCPServer {
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(5566); //创建一个Socket服务器,监听5566端口
int i=0;
//利用死循环不停的监听端口
try {
while(true){
Socket s = ss.accept();//利用Socket服务器的accept()方法获取客户端Socket对象。
i++;
System.out.println("第" + i +"个客户端成功连接!");
Client c = new Client(i,s); //创建客户端处理线程对象
Thread t =new Thread(c); //创建客户端处理线程
t.start(); //启动线程
}
} catch (Exception e) {
System.out.println("01"+e.getMessage());
}
}
} //客户端处理线程类(实现Runnable接口)
class Client implements Runnable{
int clientIndex = 0; //保存客户端id
Socket s = null; //保存客户端Socket对象
Client(int i,Socket s){
clientIndex = i;
this.s = s;
}
public void run(){
//打印出客户端数据
try{
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = null;
dos = new DataOutputStream(s.getOutputStream()); //获取Socket对象的输出流,并且在外边包一层DataOutputStream管道,方便输出数据
dos.writeUTF("服务器的数据拉"); //DataOutputStream对象的writeUTF()方法可以输出数据,并且支持中文
//dos.flush(); //确保所有数据都已经输出
if (dis.readUTF().equals("1")){
System.out.println("第" + clientIndex + "个客户端发出消息:" + dis.readUTF());
}
//dis.close();
//s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
可以了,我在服务器端加了个while,还有一个疑问如何区别是不是在上一次的基础之上有了新数据?然后在发数据到客户端?
3. 这样是否有性能问题?
如果客户端数量不会太多,这种模型就没啥问题;如果数量太多,服务端会因为线程数太多而运转不灵,那么可以启用NIO的模型。