希望看一下我在这个到底有什么问题 找一天了。。。
package come.lala;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serversocket=new ServerSocket(8888);
Socket socket=null;
int count=0;
//调用accept()方法 对指定端口进行侦查 等待客户端输入
System.out.println("***服务器等待开启***");
while(true){
socket=serversocket.accept();
//创建一个新的线程
ServerThread serverThread=new ServerThread(socket);
serverThread.start();
count++;
System.out.println("客户端的数量为:"+count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package come.lala;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws Exception {
try {
Socket socket = new Socket("192.168.1.100", 8888);
OutputStream os=socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.write("帐号:admin;密码:123");
socket.shutdownOutput();
InputStream is=socket.getInputStream();
InputStreamReader isr =new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String a=null;
while((a=br.readLine())!=null){
System.out.println("我是客户端,服务端说:"+a);
}
socket.shutdownInput();
pw.flush();
os.close();
br.close();isr.close();
is.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package come.lala;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThread extends Thread {
Socket socket=null;
public ServerThread(Socket newSocket) {
this.socket=newSocket;
}
public void run (){
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
OutputStream os=null;
PrintWriter pw=null;
try {
is = socket.getInputStream();
isr =new InputStreamReader(is);
br=new BufferedReader(isr);
String s=null;
while((s=br.readLine())!=null){
System.out.println(s);
}
socket.shutdownInput();
os=socket.getOutputStream();
pw=new PrintWriter(os);
pw.write("欢迎您客户端");
pw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pw!=null)
pw.close();
if(os!=null)
os.close();
if(socket!=null)
socket.close();
if(is!=null)
is.close();
if(br!=null)
br.close();
if(isr!=null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}