码迷,mamicode.com
首页 > 编程语言 > 详细

Java编程

时间:2015-05-01 21:13:23      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

一、客户端(Client),服务器(Server)

1.Java编程的两个概念:ServerSocket,Server。

2.套接字Socket:服务端和客户端之间通过套接字Socket建立连接,然后进行通信。

3.ServerSocket和Server的用处:首先ServerSocket将在服务端监听某个端口,当发现客户端有Socket来试图连接它时,它会使用方法accept()来接收Socket的连接请求,同时在服务端建立一个对应的Socket与之进行通信。这样就有两个Socket了,客户端和服务端各一个。同时Socket与Socket之间是双向连通的。

 

二、客户端读写和服务器读写

服务端代码

package com.lovo.chat;

import java.awt.*;
import java.io.*;
import java.net.*; import javax.swing.*;
public class Server extends JFrame { private JTextArea info; private Socket socket;
//带c参的构造器
public Server(String title) { super(title); setSize(200, 300);//设置窗口大小
setLocation(
200, 300); //设置坐标
setDefaultCloseOperation(EXIT_ON_CLOSE);//终止程序
info
= new JTextArea(); info.setEditable(false);//设置文本框是否能够编辑
info.setFocusable(
false);//设置是否聚集焦点
JScrollPane jsp
= new JScrollPane(info);//滚动面板
this.add(jsp, BorderLayout.CENTER); JPanel pnl = new JPanel(new BorderLayout()); final JTextField msg = new JTextField(); // 输入文本框 JButton send = new JButton("发送"); pnl.add(msg, BorderLayout.CENTER); pnl.add(send, BorderLayout.EAST); this.add(pnl, BorderLayout.SOUTH); setVisible(true);//设置是否窗口可见
startServer();
// 发送消息到客户端 send.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { BufferedOutputStream out = new BufferedOutputStream(socket .getOutputStream()); out.write((msg.getText() + "\n").getBytes()); out.flush(); } catch (IOException ex) { throw new RuntimeException(ex); } } }); } private void startServer() { try { ServerSocket server = new ServerSocket(30001); socket = server.accept(); // 从客户端读 read(); } catch (IOException e) { throw new RuntimeException(e); } } private void read() { try { final BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); new Thread(new Runnable() { @Override public void run() { char[] ch = new char[1024]; int len; try { while (-1 != (len = reader.read(ch))) { info.append(new String(ch, 0, len)); } } catch (IOException e) { throw new RuntimeException(e); } } }).start(); } catch (IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) { new Server("服务器"); } }
 客户端代码
package com.lovo.chat; import java.awt.
*; import java.io.*; import java.net.*; import javax.swing.*; public class Client extends JFrame { private JTextArea info; private Socket socket; public Client(String title) { super(title); setSize(200, 300); setLocation(600, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); info = new JTextArea(); info.setEditable(false); info.setFocusable(false); JScrollPane jsp = new JScrollPane(info); this.add(jsp, BorderLayout.CENTER); JPanel pnl = new JPanel(new BorderLayout()); final JTextField msg = new JTextField(); JButton send = new JButton("发送"); pnl.add(msg, BorderLayout.CENTER); pnl.add(send, BorderLayout.EAST); this.add(pnl, BorderLayout.SOUTH); setVisible(true); startClient(); // 发送消息到服务端 send.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { BufferedOutputStream out = new BufferedOutputStream(socket .getOutputStream());//写入要输出的字符
out.write((msg.getText() + "\n").getBytes()); out.flush(); } catch (IOException ex) { throw new RuntimeException(ex); } } }); } private void startClient() { try { socket = new Socket("127.0.0.1", 30001);//设置IP地址和端口号
// 从服务器读 read(); } catch (UnknownHostException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } private void read() { try { final BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); new Thread(new Runnable() { @Override public void run() { char[] ch = new char[1024]; int len; try { while (-1 != (len = reader.read(ch))) { info.append(new String(ch, 0, len)); } } catch (IOException e) { throw new RuntimeException(e); } } }).start(); } catch (IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) { new Client("客户端");//设置客户端标题
} }

 

Java编程

标签:

原文地址:http://www.cnblogs.com/g398429959/p/4471143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!