欢迎来到一句话经典语录网
我要投稿 投诉建议
当前位置:一句话经典语录 > 心得体会 > java聊天室实训心得体会

java聊天室实训心得体会

时间:2016-10-07 20:45

java聊天室思路是什么,报告怎么写

import java.net.Socket;import java.io.*;public class Client extends Thread \\\/\\\/ 定义一个Client线程类{ private Socket m_socket;\\\/\\\/ 定义一个socket对象; public Client()\\\/\\\/ 默认构造函数 { } public boolean ConnToServer(String strip, int port) throws Exception\\\/\\\/ 连接指定IP和端口的服务器,如果连接成功,返回true,否则返回false。

{ m_socket = new Socket(strip, port); return m_socket.isClosed(); } class SendData extends Thread { Socket m_sendso; BufferedReader in; OutputStream out; public SendData(Socket so) { this.m_sendso = so; this.start(); } public void run() { \\\/\\\/ 不断从键盘获取字符串发送给服务器 String msg; while (true) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = m_sendso.getOutputStream(); msg = in.readLine(); BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(out)); \\\/\\\/ new ObjectOutputStream(out).writeUTF(msg); bWriter.write(msg); } catch (IOException e) { \\\/\\\/ TODO Auto-generated catch block e.printStackTrace(); } } } } class RecvData extends Thread { Socket m_recvso; BufferedReader in; OutputStream out; public RecvData(Socket so) { this.m_recvso = so; start(); } public void run() { \\\/\\\/ 不断从服务器读取数据,并打印出来 while(true) try { in = new BufferedReader(new InputStreamReader(m_recvso.getInputStream())); System.out.println(in.readLine()); } catch (IOException e) { \\\/\\\/ TODO Auto-generated catch block e.printStackTrace(); } } }}好长时间没网络编程了,试试吧

Java网络聊天室实训能力测试

import java.net.Socket;import java.io.*;public class Client extends Thread \\\/\\\/ 定义一个Client线程类{ private Socket m_socket;\\\/\\\/ 定义一个socket对象; public Client()\\\/\\\/ 默认构造函数 { } public boolean ConnToServer(String strip, int port) throws Exception\\\/\\\/ 连接指定IP和端口的服务器,如果连接成功,返回true,否则返回false。

{ m_socket = new Socket(strip, port); return m_socket.isClosed(); } class SendData extends Thread { Socket m_sendso; BufferedReader in; OutputStream out; public SendData(Socket so) { this.m_sendso = so; this.start(); } public void run() { \\\/\\\/ 不断从键盘获取字符串发送给服务器 String msg; while (true) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = m_sendso.getOutputStream(); msg = in.readLine(); BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(out)); \\\/\\\/ new ObjectOutputStream(out).writeUTF(msg); bWriter.write(msg); } catch (IOException e) { \\\/\\\/ TODO Auto-generated catch block e.printStackTrace(); } } } } class RecvData extends Thread { Socket m_recvso; BufferedReader in; OutputStream out; public RecvData(Socket so) { this.m_recvso = so; start(); } public void run() { \\\/\\\/ 不断从服务器读取数据,并打印出来 while(true) try { in = new BufferedReader(new InputStreamReader(m_recvso.getInputStream())); System.out.println(in.readLine()); } catch (IOException e) { \\\/\\\/ TODO Auto-generated catch block e.printStackTrace(); } } }}好长时间没网络编程了,试试吧

java聊天室思路是什么,报告怎么写

无数据库版本;================================= Client =================================import java.awt.*;import java.awt.event.*;import java.net.*;import java.io.*;@SuppressWarnings(serial)public class ChatClient extends Frame {TextField tfText = new TextField();TextArea taText = new TextArea();Socket s = null;DataOutputStream dos = null;DataInputStream dis = null;public static void main(String[] args) {new ChatClient().launchFrame();}ChatClient() {super(Chat Client v1.1);}public void launchFrame() {setBounds(200, 200, 400, 300);setResizable(false);add(taText, BorderLayout.NORTH);add(tfText, BorderLayout.SOUTH);pack();taText.setEditable(false);tfText.addActionListener(new tfTextListener());addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {disConnect();System.exit(0);}});setVisible(true);connect();new Thread(new StrGetIn()).start();}private void connect() {try {s = new Socket(127.0.0.1, 24031);dos = new DataOutputStream(s.getOutputStream());dis = new DataInputStream(s.getInputStream());} catch (IOException e) {System.out.println(服务器不存在或系统错误,请联系管理员,系统退出...);System.exit(-1);}}private void disConnect() {try {dos.writeUTF(Client exit...);dos.close();dis.close();s.close();} catch (IOException e1) {System.out.println(无法关闭网络链接,系统非法退出...);}}private class tfTextListener implements ActionListener {public void actionPerformed(ActionEvent e) {String str = tfText.getText().trim();try {if (!str.equals()) {\\\/\\\/taText.append(str + \\\ );dos.writeUTF(str);dos.flush();} else {taText.append(系统提示:****发送内容不能为空****\\\ );}} catch (IOException e1) {System.out.println(网络连接错误,请联系管理员,系统退出...);System.exit(-1);}tfText.setText();}}private class StrGetIn implements Runnable {public void run() {try {while(true) {taText.append(dis.readUTF());}} catch (IOException e) {System.out.println(Client readUTF error);}}}}

声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。联系xxxxxxxx.com

Copyright©2020 一句话经典语录 www.yiyyy.com 版权所有

友情链接

心理测试 图片大全 壁纸图片