关于SOCKET通信的问题,怎样结束本次发送的字符?

superzyxun 2004-08-13 10:00:40
想用SOCKET做一个简单的聊天工具练手,现在东西做出来了,可是在发送数据的时候,客户端在用ReadLine方法读取数据时,程序就停止不动了,查看资料发现是接收到的数据没有换行符,程序一直在等读完一行才运行,我试着在发送数据的时候在结束时加上“\n”,可是还是不行,应该怎么在发送数据后给数据一个换行符?
...全文
670 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
solares1 2004-11-05
  • 打赏
  • 举报
回复
gz
drinkant 2004-08-13
  • 打赏
  • 举报
回复
flush() !
SBNOone 2004-08-13
  • 打赏
  • 举报
回复
ObjectInputStream ooi = new ObjectInputStream (netSocket.getInputStream())

Object obj = ooi.readObject();//从套接字里面读


ObjectOutpubStream oop = new ObjectInputStream(netSocket.getOutputStream())

oop.writeObject(obj);//写到套接字里面

这里读来读去的全是对象
SBNOone 2004-08-13
  • 打赏
  • 举报
回复
可以用
ObjectInputStream ooi = new ObjectInputStream (netSocket.getInputStream())从套接字里面读

ObjectOutpubStream oop = new ObjectInputStream(netSocket.getOutputStream())写到套接字里面
superzyxun 2004-08-13
  • 打赏
  • 举报
回复
有人可以解答一下吗,或者不用ReadLine方法,有没有别的读取数据的方法?
superzyxun 2004-08-13
  • 打赏
  • 举报
回复
我记得\n才是换行符,\t好像是TAB键吧。
反正\t我试了还是不行的。
lixiang823517 2004-08-13
  • 打赏
  • 举报
回复
\t试试
superzyxun 2004-08-13
  • 打赏
  • 举报
回复
下面是Client端:
import java.net.*;
import java.io.*;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
/**
* @author administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SockFrm extends JFrame{
private JTextArea Txtaa = new JTextArea(4,20);
private JTextField Txtfd = new JTextField(20);
private JButton JBon = new JButton("发送");
private String IPstr;
private SockFrm SF;
private Socket soc=null;
private PrintStream out=null;
private BufferedReader in;
private BufferedReader stdIn;

public SockFrm(){
super("网络聊天实例");
Container cont = getContentPane();
cont.setLayout(new FlowLayout());
JLabel lab1 = new JLabel("聊天记录:");
JLabel lab2 = new JLabel("你的发言:");
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu FileMenu = new JMenu("文件");
JMenuItem ConSerMenu = new JMenuItem("连接服务器");
ConSerMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent Event){
IPFrame IPF = new IPFrame();
IPF.setParent(SF);
//IPF.setDefaultCloseOperation(IPFrame.EXIT_ON_CLOSE);
}
});
FileMenu.add(ConSerMenu);
bar.add(FileMenu);
cont.add(lab1);
cont.add(Txtaa);
cont.add(lab2);
cont.add(Txtfd);
cont.add(JBon);
setSize(280,260);
setVisible(true);
SF=this;
JBon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
out.println(Txtfd.getText()+"\n");
out.flush();
}
});
}

public void ConServer(String IP){
String inputLine;
IPstr=IP;
int Num=0;
//JOptionPane.showMessageDialog(null,IPstr,"系统提示",JOptionPane.INFORMATION_MESSAGE);
try{
soc = new Socket(IPstr,9494);
out = new PrintStream(soc.getOutputStream());
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
//stdIn = new BufferedReader(new InputStreamReader(System.in));
while((Num=in.read())!=0){
char[] cBuf = new char[Num];
in.read(cBuf);
inputLine= new String(cBuf);
Txtaa.setText(Txtaa.getText()+inputLine);
}
}catch (IOException e){
System.out.println(e.getMessage());
}
}

public static void main(String args[]){
SockFrm SF= new SockFrm();
SF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

还有一个窗口用来输入连接服务器地址:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* @author administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class IPFrame extends JFrame{
private String IPadd;
private JTextField JTF = new JTextField(15);
private JButton JBon1 = new JButton("确定");
private JButton JBon2 =new JButton("取消");
private SockFrm isPa;

public IPFrame(){
super("输入IP地址");
Container Cont = getContentPane();
Cont.setLayout(new FlowLayout());
JLabel lab1 = new JLabel("服务器IP:");
Cont.add(lab1);
Cont.add(JTF);
Cont.add(JBon1);
Cont.add(JBon2);
ButtonHandle handle = new ButtonHandle();
JBon1.addActionListener(handle);
JBon2.addActionListener(handle);
setSize(300,100);
setVisible(true);
}

public void setParent(SockFrm SF){
isPa = SF;
}

private class ButtonHandle implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource()==JBon1){
IPadd = JTF.getText();
dispose();
isPa.ConServer(IPadd);
}else if(event.getSource()==JBon2){
dispose();
}

}
}
}
superzyxun 2004-08-13
  • 打赏
  • 举报
回复
受不了了,我加了flush(),也改用byte接收,可是还是出不来。我把代码贴出来给大家吧,请大家给我看看:
Server端:
import javax.swing.*;

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**
* @author administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Svr_frm extends JFrame{
private JTextArea Txtaa = new JTextArea(4,20);
private JTextField Txtfd = new JTextField(20);
private JButton JBon = new JButton("发送");
private ServerSocket Svrsoc = null;
private PrintStream out=null;

public Svr_frm(){
super("网络聊天实例");
Container cont = getContentPane();
cont.setLayout(new FlowLayout());
JLabel lab1 = new JLabel("聊天记录:");
JLabel lab2 = new JLabel("你的发言:");
cont.add(lab1);
cont.add(Txtaa);
cont.add(lab2);
cont.add(Txtfd);
cont.add(JBon);
setSize(280,260);
setVisible(true);

JBon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
out.println(Txtfd.getText()+"\n");
out.flush();
Txtaa.setText(Txtaa.getText()+"\n" +Txtfd.getText());
Txtfd.setText("");
}
});
try{
Svrsoc = new ServerSocket(9494);
int Num=0;
while(true){
Socket newSoc= Svrsoc.accept();
out = new PrintStream( newSoc.getOutputStream( ) );
BufferedReader in = new BufferedReader( new InputStreamReader( newSoc.getInputStream( ) ) );
if((Num=in.read())!=0){
char[] cBuf = new char[Num];
in.read(cBuf);
String pass = new String(cBuf);
Txtaa.setText(Txtaa.getText()+pass);
}
}
}catch(IOException e){
System.out.println(e.getMessage());
}
}

public static void main(String args[]){
Svr_frm newSvr= new Svr_frm();
newSvr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
qqbz 2004-08-13
  • 打赏
  • 举报
回复
网络程序全部用byte收发。
pigrain 2004-08-13
  • 打赏
  • 举报
回复
记住要flush()
southforest 2004-08-13
  • 打赏
  • 举报
回复
我以前也碰到你这样的问题,然后我就不用readLine了,而用read()就可以了.

private BufferedReader bfReader;

bfReader = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
char[] cBuf = new char[100];
bfReader.read(cBuf);

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧