女侠:)昨天忘了给你传你程序上来 忙别的了 这给你传上来给你大改了下
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.naming.ldap.StartTlsRequest;
public class Server implements Runnable{
private static ServerSocket serversocket;
private static Socket socket=null;
private static BufferedReader read;
private static PrintWriter writer;
private InetAddress address;
private int port;
public Server(){
try{
serversocket=new ServerSocket(3000);
System.out.println("服务器启动\n");
}catch(Exception e){
}
}
public void run() {
while(true){
String temp=null;
try {
read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
temp = read.readLine();
System.out.println("Server receive:"+temp);
if(temp.equals("bye")){
try{
serversocket.close();
writer.close();
}catch(Exception ee){
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void send(){
try {
writer = new PrintWriter(socket.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(System.in
));
System.out.println("发送信息:");
String s=in.readLine();
while(!s.equals("bye")){
System.out.println("输入发送信息,输入bye退出");
writer.println(s);
writer.flush();
s=in.readLine();
}
writer.println("bye");
writer.flush();
serversocket.close();
in.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String srag[]){
Server s=new Server();
while(true){
try{
socket=serversocket.accept();
new Thread(s).start();
send();
}catch(Exception e){
}
}
}
}
000000000000000000000client000000000000000000000
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client implements Runnable{
Socket socket;
PrintWriter out;
BufferedReader in;
public Client(){
try{
socket=new Socket("127.0.0.1",3000);
System.out.println("客户启动开始监听接受数据");
}catch(Exception e){
}
new Thread(this).start();
}
public void send(){
try {
out = new PrintWriter(socket.getOutputStream());
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String s=reader.readLine();
while(!s.equals("bye")){
System.out.println("输入发送数据,bye退出");
out.println(s);
out.flush();
s=reader.readLine();
}
out.println("bye");
out.flush();
out.close();
in.close();
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while(true){
String temp=null;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
temp = in.readLine();
System.out.println("Client receive:"+temp);
if(temp.equals("bye")){
try{
out.close();
in.close();
socket.close();
}catch(Exception ee){
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String args[]){
Client c=new Client();
c.send();
}
}