cilent客户端。。
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class chatclient extends Frame{
Socket s = null;
TextField tf = new TextField();
TextArea ta = new TextArea();
Calendar c = Calendar.getInstance();
OutputStreamWriter osr = null ;
InputStreamReader isr = null;
BufferedReader br = null ;
String str = null;
boolean beconnect = false ;
public static void main(String[] args) {
new chatclient().launch();
}
public void launch(){
ta.setEditable(false);
this.setLocation(200,200);
this.setSize(400, 350);
this.add(ta,BorderLayout.NORTH);
this.add(tf,BorderLayout.SOUTH);
pack();
this.setVisible(true);
this.addWindowListener(new closeWindow());
tf.addKeyListener(new keyEvent());
connected();
receiveMail r = new receiveMail();
Thread t = new Thread(r);
t.start();
}
public void connected(){
try {
s = new Socket("127.0.0.1",8888);
osr = new OutputStreamWriter(s.getOutputStream());//向服务器发送信息
isr = new InputStreamReader(s.getInputStream());//从服务端接受信息
br = new BufferedReader(isr);
System.out.println("conneted");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
beconnect = true ;
}
public void disconnected(){
try {
osr.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class closeWindow extends WindowAdapter{
public void windowClosing(WindowEvent e){
disconnected();
System.exit(0);
}
}
class keyEvent extends KeyAdapter{
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
TextField t =(TextField) e.getSource();
ta.append( " client1:" +t.getText() +" "+getTime() +'\n');
try {
osr.append( " client:" +t.getText() +" "+getTime() +'\n');
osr.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
public String getTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
return df.format(new Date());// new Date()为获取当前系统时间
}
class receiveMail implements Runnable{
public void run() {
while(beconnect){
try {
System.out.print(br.readLine());//读出从服务端接受的信息
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端。。
import java.io.*;
import java.net.*;
import java.util.*;
public class chatsevice {
public static int n = 0;
public static ServerSocket ss = null ;
public static boolean started = false ;
List <cilent> cilents = new ArrayList<cilent>(); public static void main(String[] args){
new chatsevice().start();
}
public void start(){
try{
ss = new ServerSocket(8888);
started = true ;
}catch(BindException e){
System.out.println("端口被占用!");
}catch(IOException e){
e.printStackTrace();
}
try{
Socket s ;
while(started){
s =ss.accept();
cilent c = new cilent(s);
Thread t = new Thread(c);
cilents.add(c);
System.out.println("a client connected!"+n++);
c.bconnected = true ;
t.start();
}
}catch(IOException e){
e.printStackTrace();
}
}
class cilent implements Runnable{
private Socket s =null ;
public InputStreamReader iso = null ;
public OutputStreamWriter osr = null ;
public BufferedReader br = null ;
public String str = null ;
public boolean bconnected = false ;
public cilent(Socket s){
this.s = s;
try {
iso = new InputStreamReader(s.getInputStream());
osr = new OutputStreamWriter(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
br = new BufferedReader(iso);
}
public void send(String str){
try {
osr.write(str);
} catch (IOException e) {
System.out.println("发送失败");
}
}
public void run() {
try {
if(bconnected){
while((str=br.readLine())!=null){
System.out.println(str);
for(int i=0;i<cilents.size();i++){
cilent c = cilents.get(i);
c.send(str);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端就是接受不到服务端的数据啊,怎么回事~~~
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class chatclient extends Frame{
Socket s = null;
TextField tf = new TextField();
TextArea ta = new TextArea();
Calendar c = Calendar.getInstance();
OutputStreamWriter osr = null ;
InputStreamReader isr = null;
BufferedReader br = null ;
String str = null;
boolean beconnect = false ;
public static void main(String[] args) {
new chatclient().launch();
}
public void launch(){
ta.setEditable(false);
this.setLocation(200,200);
this.setSize(400, 350);
this.add(ta,BorderLayout.NORTH);
this.add(tf,BorderLayout.SOUTH);
pack();
this.setVisible(true);
this.addWindowListener(new closeWindow());
tf.addKeyListener(new keyEvent());
connected();
receiveMail r = new receiveMail();
Thread t = new Thread(r);
t.start();
}
public void connected(){
try {
s = new Socket("127.0.0.1",8888);
osr = new OutputStreamWriter(s.getOutputStream());//向服务器发送信息
isr = new InputStreamReader(s.getInputStream());//从服务端接受信息
br = new BufferedReader(isr);
System.out.println("conneted");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
beconnect = true ;
}
public void disconnected(){
try {
osr.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class closeWindow extends WindowAdapter{
public void windowClosing(WindowEvent e){
disconnected();
System.exit(0);
}
}
class keyEvent extends KeyAdapter{
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
TextField t =(TextField) e.getSource();
ta.append( " client1:" +t.getText() +" "+getTime() +'\n');
try {
osr.append( " client:" +t.getText() +" "+getTime() +'\n');
osr.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
public String getTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
return df.format(new Date());// new Date()为获取当前系统时间
}
class receiveMail implements Runnable{
public void run() {
while(beconnect){
try {
System.out.print(br.readLine());//读出从服务端接受的信息
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端。。
import java.io.*;
import java.net.*;
import java.util.*;
public class chatsevice {
public static int n = 0;
public static ServerSocket ss = null ;
public static boolean started = false ;
List <cilent> cilents = new ArrayList<cilent>(); public static void main(String[] args){
new chatsevice().start();
}
public void start(){
try{
ss = new ServerSocket(8888);
started = true ;
}catch(BindException e){
System.out.println("端口被占用!");
}catch(IOException e){
e.printStackTrace();
}
try{
Socket s ;
while(started){
s =ss.accept();
cilent c = new cilent(s);
Thread t = new Thread(c);
cilents.add(c);
System.out.println("a client connected!"+n++);
c.bconnected = true ;
t.start();
}
}catch(IOException e){
e.printStackTrace();
}
}
class cilent implements Runnable{
private Socket s =null ;
public InputStreamReader iso = null ;
public OutputStreamWriter osr = null ;
public BufferedReader br = null ;
public String str = null ;
public boolean bconnected = false ;
public cilent(Socket s){
this.s = s;
try {
iso = new InputStreamReader(s.getInputStream());
osr = new OutputStreamWriter(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
br = new BufferedReader(iso);
}
public void send(String str){
try {
osr.write(str);
} catch (IOException e) {
System.out.println("发送失败");
}
}
public void run() {
try {
if(bconnected){
while((str=br.readLine())!=null){
System.out.println(str);
for(int i=0;i<cilents.size();i++){
cilent c = cilents.get(i);
c.send(str);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端就是接受不到服务端的数据啊,怎么回事~~~
改为osr.write(str + "\n");
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
Socket s=null;
DataOutputStream dos=null;
DataInputStream dis=null;
private boolean bConnected=false; TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); public static void main(String[] args) {
new ChatClient().launchFrame();
} public void launchFrame() { //加载窗体
setLocation(400, 300); //设置窗体属性
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH); //加载部件
add(taContent, BorderLayout.NORTH);
pack(); //取消没有使用的部分
this.addWindowListener(new WindowAdapter(){ //实现退出按钮 public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener()); //在Area中显示输入的内容并传送给服务端
setVisible(true);
connect(); //连接
new Thread (new RecvThread()).start();
}
public void connect(){ //建立连接
try {
s=new Socket("127.0.0.1",8888);
System.out.println("connected");
dos=new DataOutputStream(s.getOutputStream());
dis= new DataInputStream(s.getInputStream());
System.out.println("connected");
bConnected=true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect(){ //关闭连接
try {
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener{ public void actionPerformed(ActionEvent e) {
String str=tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");
try { //传送数据
dos.writeUTF(str);
dos.flush();
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
} private class RecvThread implements Runnable{ public void run() {
try{
while(bConnected){
String str=dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText()+str+'\n');
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;public class ChatServer
{
boolean started = false; //是否连接
ServerSocket ss=null;
List<Client> clients = new ArrayList<Client>(); public static void main(String[] args)
{
new ChatServer().start(); //111
}
public void start()
{ //222
try
{
ss=new ServerSocket(8888);
started=true; //333
}
catch(BindException e)
{
System.out.println("端口使用中……");
System.out.println("请关掉相关程序并重新运行服务器");
System.exit(0);
}
catch(IOException e)
{
e.printStackTrace();
}
try
{ //444
while(started)
{
Socket s=ss.accept();
Client c=new Client(s); //555
System.out.println("a client connected");
new Thread(c).start(); //777
clients.add(c);
//dis.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
ss.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
} class Client implements Runnable //线程类
{
private Socket s;
private DataInputStream dis=null;
private DataOutputStream dos =null;
private boolean bConnected=false;
public Client(Socket s)
{ //构造方法 //666
this.s=s;
try
{
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
bConnected=true;
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void send(String str){
try {
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run()
{ //run方法 //888
try
{
while(bConnected)
{
String str=dis.readUTF(); //阻塞性方法,没有消息时,程序停止
System.out.println(str);
for(int i=0;i<clients.size();i++)
{
Client c= clients.get(i);
c.send(str);
}
}
}
catch(EOFException e)
{
System.out.println("Client Closed");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) s.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}
}