
正文
java网络编程源代码 java基于nio网络编程代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java网络编程,客户端和服务端相互发送信息,可是我写的双方都没读到对方发送的信息,代码如下
这个地方有问题,第一,你必须写入newLine,要不会造成阻塞;第二,你必须先flush后才能读服务器;第三,你从控制台输入应该有个结束标志,要不你在while里面把bw关闭了,你还怎么循环
客户端这个地方重写一下,
while(true) {
temp=br.readLine();
if(temp.equals("over")){
break;
}
bw.write(temp);
bw.newLine();//如果没有,使用readLine则会造成莫名等待
bw.flush();
}
不一定改全了,你先试试这些改动,如有问题再找
相关问答
Q1: 求一个用JAVA写的网络编程的网络聊天系统,能够实现两个人聊天信息收发。
这个是客户端
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class client extends Frame implements ActionListener{
int i=1;Frame f;
TextField ip,port;
Label Lip,Lport;
Button connect,exit;
public static void main(String args[])
{client c1 = new client();
c1.init();
}
public void init()
{
f=new Frame("client connection");
f.setLayout(new FlowLayout());
ip =new TextField("localhost");
port =new TextField("8189",5);
Lip=new Label("ip address");
Lport=new Label("port");
connect=new Button("connect");
exit=new Button("exit");
connect.addActionListener(this);
exit.addActionListener(this);
f.add(Lip);
f.add(ip);
f.add(Lport);
f.add(port);
f.add(connect);
f.add(exit);
f.setSize(500,60);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==exit)
System.exit(0);
if(e.getSource()==connect)
{
new Thread(new threadclient(ip.getText(),port.getText(),i)).start();
i++;
}
}
}
class threadclient extends Frame implements Runnable,ActionListener{
String ip,port;
int no;
Frame f;
TextArea ta;
TextArea name;
TextField tf;
Button send,bye;
InputStream ins;
Socket s;
PrintStream out1;
BufferedReader in;
BufferedWriter out;
threadclient(String n,String m,int i)
{
ip=n;
port=m;
no=i;
}
public void run(){
f=new Frame("client NO." +no);
f.setLayout(new FlowLayout());
ta=new TextArea("",10,30,TextArea.SCROLLBARS_BOTH);
tf=new TextField("",20);
send=new Button("send");
bye=new Button("bye");
send.addActionListener(this);
bye.addActionListener(this);
f.add(ta);
f.add(tf);
f.add(send);
f.add(bye);
f.setSize(300,300);
f.setVisible(true);
Integer tmp=new Integer(port);
int portint =tmp.intValue();
try
{
s=new Socket(ip,portint);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out1=new PrintStream(s.getOutputStream());
ta.append(in.readLine()+"\n");
}catch(Exception e)
{
System.out.println(e.getMessage()+" ss");
}
}
public void send(String txt){
try{
out1.println(txt);
out1.flush();
ta.append(in.readLine()+"\n");
}catch(IOException e)
{
System.out.println(e.getMessage()+"send");
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bye){
send("BYE");
System.exit(0);
}
if (e.getSource()==send)
send(tf.getText());
}
}
这个是服务器
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class server {
private static MapString,Socket clientMap=new HashMapString,Socket();
public static void main(String[] args) {
int i = 1;
try {
ServerSocket s = new ServerSocket(8189);
for (;;) {
Socket incoming = s.accept();
System.out.println("连接成功" + i);
ThreadedEchoHandler teh=new ThreadedEchoHandler(incoming, i);
teh.start();
String name=teh.getClientname();
clientMap.put(name,incoming);
i++;
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class ThreadedEchoHandler extends Thread {
Frame f;
TextArea ta;
TextField tf;
Button send, bye;
InputStream ins;
Socket s;
PrintStream out1;
BufferedReader in;
PrintWriter out;
public ThreadedEchoHandler(Socket i, int c) {
incoming = i;
counter = c;
f=new Frame("server");
f.setLayout(new FlowLayout());
ta=new TextArea("",10,30,TextArea.SCROLLBARS_BOTH);
tf=new TextField("",20);
send=new Button("send");
bye=new Button("bye");
send.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
send(tf.getText());
}
});
bye.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
send("bye");
}
});
f.add(ta);
f.add(tf);
f.add(send);
f.add(bye);
f.setSize(300,300);
f.setVisible(true);
}
public String getClientname() {
try {
in = new BufferedReader(new InputStreamReader(
incoming.getInputStream()));
out = new PrintWriter(incoming.getOutputStream(), true);
return in.readLine();
} catch (IOException e) {
System.out.println(e.getMessage()+"get");
}
return null;
}
public void send(String context){
out.println(context);
out.flush();
}
public void run() {
try {
boolean done = false;
while (!done) {
String str = in.readLine();
if (str == null)
done = true;
else {
out.println("Echo(" + counter + "):" + str);
ta.append("Echo(" + counter + "):" + str+"\n");
if (str.trim().equals("BYE"))
done = true;
}
}
incoming.close();
} catch (Exception e) {
System.out.println(e.getMessage()+"run");
}
}
private Socket incoming;
private int counter;
}
这个鸟东西是个新手写java网络编程源代码的。唉java网络编程源代码,太烂java网络编程源代码了,java网络编程源代码我无力吐槽。
Q2: java网络编程问题:
你搜索java网络编程源代码的关键字不对, 如果你在百度搜“socket模拟Http请求头部” 得到的第一个结果就是你想要的答案。
比如java网络编程源代码:
这里java网络编程源代码我再跟你解说一下java网络编程源代码:
所先搞清楚http和socket连接有什么区别,再解决这些区别冲突就可以模仿java网络编程源代码了。
1。 区别1: http是无状态的,每次请求完收到数据就必须关断连接。 而Socket不要关断连接,可以连续发送消息和获取消息。后者甚至可以开两个线程一个读一个写。 所以,这里我们要每次在Socket请求完后就关断,或者模似让后台关断。
2。 区别2 : Http因为无状态,所以为了让后台与前台相互确认,就需要HTTP头部信息来进行双方确认。 这些信息包括content-type,content-length,responseCode(200,306,404等), 而Socket不需要且没有这些头部信息。 所以问题的关键就是如果在Socket请求通道中加入这些头部信息,有了这些信息,后台就会认为这是HTTP请求了。
具体方法我就不码字了,我上面有链接,你可以看到请详细的代码,也可以再用百度搜那个关键字
很久以前,我们做手机游戏时, 当时如果不是中国移动全球通的卡的话就只充许使用CMWAP连接,不充许使用CMNET连接, 两者区别是CMNET可以使用SOCKET,而CMWAP中使用SOCKET的话会被主机拒绝连接。 所以我们就想了楼主你这个一样的点子。在CMWAP中开一个SOCKET连接到移动主机,但是在SOCKET的请求中加入了HTTP头部信息,结果移动主机以为我是HTTP连接,从而骗过了移动主机成功在CMWAP中使用SOCKET。
SOCKET通信的好处有很多,速度快是最大的优势。
Q3: 一个最简单的java网络编程
java网络编程源代码你好:Socket s = new Socket("localhost", 6660);//14行
这个的话java网络编程源代码,你的没通, 去黑窗口输入 tenlet 127.0.0.1 6660;看看是否有结果输出,再就是你别用localhost了,换成实际的127.0.0.1的地址,
Q4: 关于java网络编程的基础问题,已经附带源代码
你需要用一个list 管理 所有的客户端socket 。
Socket socket = ss.accept();
socketList.add(socket )
然后 再依次向么个socket 输出
for (Iterator iterator = socketList.iterator(); iterator.hasNext();) {
Socket object = (Socket) iterator.next();
OutputStream os = socket.getOutputStream();
DataOutputStream dis = new DataOutputStream(os);
dis.writeUTF( “message”);
}
java网络编程源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java基于nio网络编程代码、java网络编程源代码的信息别忘了在本站进行查找喔。





