我是java新手 聊天程序 现在还只写了一个client端和一个server端
用client发消息总是等client关闭server才能收到消息这是为什么啊跪谢!!代码如下:client:importjava.awt.*;importjava.aw...
用client发消息 总是等client关闭 server才能收到消息 这是为什么啊 跪谢!! 代码如下:
client:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class ChatClient extends Frame{
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
static Socket s;
BufferedWriter bw = null;
public static void main(String[] args) {
ChatClient cc= new ChatClient();
cc.connect();
cc.launchFrame();
}
public void connect(){
try {
s = new Socket("127.0.0.1",9408);
System.out.println("Connected!");
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
} catch (UnknownHostException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void launchFrame(){
this.setLocation(400, 300);
this.setSize(300,300);
add(tfTxt,BorderLayout.SOUTH);
add(taContent,BorderLayout.NORTH);
pack();
this.setVisible(true);
tfTxt.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int a;
a = e.getKeyCode();
if(a == KeyEvent.VK_ENTER){
String string =tfTxt.getText().trim();
taContent.setText(string);
tfTxt.setText("");
try{
System.out.println(string);
bw.write(string);
bw.flush();
}catch(IOException ex){
System.out.println(ex);
}
}
}
});
this.addWindowListener(new MyMonitor());
}
public void disConnect(){
try {
bw.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyMonitor extends WindowAdapter{
public void windowClosing(WindowEvent e){
ChatClient f =(ChatClient) e.getSource();
f.disConnect();
System.exit(0);
}
}
server端:
import java.io.*;
import java.net.*;
public class ChatServer {
static Socket s = null;
static ServerSocket ss = null;
public static void main(String[] args) {
try {
ss = new ServerSocket(9408);
while(true){
s = ss.accept();
System.out.println("a client has connect");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String string = br.readLine();
System.out.println(string);
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} 展开
client:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class ChatClient extends Frame{
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
static Socket s;
BufferedWriter bw = null;
public static void main(String[] args) {
ChatClient cc= new ChatClient();
cc.connect();
cc.launchFrame();
}
public void connect(){
try {
s = new Socket("127.0.0.1",9408);
System.out.println("Connected!");
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
} catch (UnknownHostException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void launchFrame(){
this.setLocation(400, 300);
this.setSize(300,300);
add(tfTxt,BorderLayout.SOUTH);
add(taContent,BorderLayout.NORTH);
pack();
this.setVisible(true);
tfTxt.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int a;
a = e.getKeyCode();
if(a == KeyEvent.VK_ENTER){
String string =tfTxt.getText().trim();
taContent.setText(string);
tfTxt.setText("");
try{
System.out.println(string);
bw.write(string);
bw.flush();
}catch(IOException ex){
System.out.println(ex);
}
}
}
});
this.addWindowListener(new MyMonitor());
}
public void disConnect(){
try {
bw.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyMonitor extends WindowAdapter{
public void windowClosing(WindowEvent e){
ChatClient f =(ChatClient) e.getSource();
f.disConnect();
System.exit(0);
}
}
server端:
import java.io.*;
import java.net.*;
public class ChatServer {
static Socket s = null;
static ServerSocket ss = null;
public static void main(String[] args) {
try {
ss = new ServerSocket(9408);
while(true){
s = ss.accept();
System.out.println("a client has connect");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String string = br.readLine();
System.out.println(string);
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} 展开
4个回答
展开全部
写了个最简单的例子...注意的是client向server发送数据的时候我习惯性用了println,我记得这个是因为server用了readline这样的方法去读取.
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
Socket s = null;
ServerSocket ss = null;
try {
ss = new ServerSocket(9999);
System.out.println("listinning...");
s = ss.accept();
System.out.println("get the one ...");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
while(true){
System.out.print("read:");
System.out.println(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(s!=null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ss!=null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
Socket s = null;
try {
s = new Socket("127.0.0.1",9999);
PrintWriter pw = new PrintWriter(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.print("input something:");
pw.println(br.readLine());
pw.flush();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(s!=null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
追问
好感动 ~~ 鼻涕都出来了 我赶紧看看 谢谢谢 真的很谢谢
展开全部
问题在 你这个
public void windowClosing(WindowEvent e){
ChatClient f =(ChatClient) e.getSource();
f.disConnect();
System.exit(0);
}
方法里,当你关闭窗口的时候 流才关闭 你这个关闭流的方法 没必要写在这里 你自己琢磨琢磨怎么改。
public void windowClosing(WindowEvent e){
ChatClient f =(ChatClient) e.getSource();
f.disConnect();
System.exit(0);
}
方法里,当你关闭窗口的时候 流才关闭 你这个关闭流的方法 没必要写在这里 你自己琢磨琢磨怎么改。
追问
哦哦哦 我去看看怎么改 谢谢啊 真心谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
太长了,建议你检查一下你代码的顺序有没有问题,如果服务器端用循环一直处于阻塞待接收数据的状态,侃侃是不是输出流放在循环外面了?
追问
嗯嗯 我去看看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你应该注意线程的问题!
更多追问追答
追问
我是在看的马士兵老师的视频 但是他在写这个程序的时候没有用多线程额
追答
你是用Socket来通讯的吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询