制作java聊天小程序 如何建立客户端与服务端的连接
展开全部
这个是UDP写的
这个是客户端
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class UDPClientDemo extends JFrame{
String host="localhost"; //服务器地址
int port=2345; //端口号
public UDPClientDemo(){
try{
DatagramSocket socket=new DatagramSocket(); //实例化一个数据报Socket
InetAddress address=InetAddress.getByName(host); //服务器地址
byte[] buffer=new byte[256]; //缓冲区
DatagramPacket packet=new DatagramPacket(buffer,buffer.length,address,port); //实例化一个数据报
socket.send(packet); //发送报文
packet=new DatagramPacket(buffer,buffer.length);
socket.receive(packet); //接受回应
String message=new String(packet.getData()); //得到报文信息
System.out.println("Received from: "+packet.getAddress()); //显示信息源地址
System.out.println(message); //显示服务器返回信息
socket.close(); //关闭端口
}
catch (Exception ex){
ex.printStackTrace(); //输出出错信息
}
}
public static void main(String[] args){
new UDPClientDemo();
}
}
这个是服务器
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class UDPServerDemo extends JFrame{
int port=2345; //端口号
public UDPServerDemo(){
try{
byte[] buffer=new byte[256]; //缓冲区数组
DatagramSocket socket=new DatagramSocket(port); //实例化数据报Socket
DatagramPacket packet;
System.out.println("Server start..."); //输出运行信息
while (true){
packet=new DatagramPacket(buffer,buffer.length); //实例化数据报
socket.receive(packet); //接受请求
InetAddress target=packet.getAddress();
System.out.println("Received from"+target); //输出请求地址
int port=packet.getPort(); //得到接收端口
byte[] message="This is server,Who are you?".getBytes(); //服务器返回信息
packet=new DatagramPacket(message,message.length,target,port); //实例化数据报
socket.send(packet); //发送数据报
}
}
catch (Exception ex){
ex.printStackTrace(); //输出出错信息
}
}
public static void main(String[] args){
new UDPServerDemo();
}
}
这个其实还可以简化 做成最简单的 但是这个两个代码里面你可以学习到很多知识 很经典的代码
这个是客户端
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class UDPClientDemo extends JFrame{
String host="localhost"; //服务器地址
int port=2345; //端口号
public UDPClientDemo(){
try{
DatagramSocket socket=new DatagramSocket(); //实例化一个数据报Socket
InetAddress address=InetAddress.getByName(host); //服务器地址
byte[] buffer=new byte[256]; //缓冲区
DatagramPacket packet=new DatagramPacket(buffer,buffer.length,address,port); //实例化一个数据报
socket.send(packet); //发送报文
packet=new DatagramPacket(buffer,buffer.length);
socket.receive(packet); //接受回应
String message=new String(packet.getData()); //得到报文信息
System.out.println("Received from: "+packet.getAddress()); //显示信息源地址
System.out.println(message); //显示服务器返回信息
socket.close(); //关闭端口
}
catch (Exception ex){
ex.printStackTrace(); //输出出错信息
}
}
public static void main(String[] args){
new UDPClientDemo();
}
}
这个是服务器
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class UDPServerDemo extends JFrame{
int port=2345; //端口号
public UDPServerDemo(){
try{
byte[] buffer=new byte[256]; //缓冲区数组
DatagramSocket socket=new DatagramSocket(port); //实例化数据报Socket
DatagramPacket packet;
System.out.println("Server start..."); //输出运行信息
while (true){
packet=new DatagramPacket(buffer,buffer.length); //实例化数据报
socket.receive(packet); //接受请求
InetAddress target=packet.getAddress();
System.out.println("Received from"+target); //输出请求地址
int port=packet.getPort(); //得到接收端口
byte[] message="This is server,Who are you?".getBytes(); //服务器返回信息
packet=new DatagramPacket(message,message.length,target,port); //实例化数据报
socket.send(packet); //发送数据报
}
}
catch (Exception ex){
ex.printStackTrace(); //输出出错信息
}
}
public static void main(String[] args){
new UDPServerDemo();
}
}
这个其实还可以简化 做成最简单的 但是这个两个代码里面你可以学习到很多知识 很经典的代码
展开全部
用socket套接字。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建议你看这个,可以多用户聊天的
http://148380547-163-com.javaeye.com/blog/663487
http://148380547-163-com.javaeye.com/blog/663487
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
利用Socket编程
import java.net.*;
import java.io.*;
import java.util.*;
public class QQServer {
ServerSocket ss;
List<Client> list = new ArrayList<Client>();
boolean start ;
public static void main(String[] args) {
new QQServer().receiveMethod();
}
public void receiveMethod(){
try {
ss = new ServerSocket(8980);
start = true;
}catch (BindException e){
System.out.println("端口已经被占用");
}catch (IOException e){
e.printStackTrace();
}
try {
while (start){
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client is connected");
new Thread(c).start();
list.add(c);
}
}catch(IOException e){
e.printStackTrace();
}finally {
try {
ss.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
private class Client implements Runnable {
BufferedReader br;
PrintWriter pw;
Socket socket;
boolean connect;
public void run() {
try {
while(connect){
String s = br.readLine();
System.out.println(s);
for (int i=0; i<list.size(); i++){
Client client = list.get(i);
client.send(s);
}
}
}catch (EOFException e){
System.out.println("客户端已经退出");
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if(br != null){
br.close();
}
if(pw != null){
pw.close();
}
if(socket != null){
socket.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
public void send(String s){
pw.println(s);
pw.flush();
}
public Client(Socket socket){
this.socket = socket;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
connect = true;
}catch (IOException e){
e.printStackTrace();
}
}
}
}
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class QQClient extends Frame {
Socket socket;
BufferedReader br;
PrintWriter pw;
boolean start;
TextField tf = new TextField();
TextArea ta = new TextArea();
public static void main(String[] args) {
new QQClient().launchFrame();
}
public void launchFrame(){
setBounds(400,300,300,300);
add(ta,BorderLayout.NORTH);
add(tf, BorderLayout.SOUTH);
tf.addActionListener(new Monitor());
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
setVisible(true);
try {
socket = new Socket("127.0.0.1", 8980);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
start = true;
System.out.println("kk");
}catch (UnknownHostException e){
System.out.println("如果无法确定主机的 IP 地址");
}catch (IOException e){
e.printStackTrace();
}
new Thread(new Server()).start();
}
public void disconnect(){
try {
br.close();
pw.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
}
private class Monitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s = tf.getText();
tf.setText("");
pw.println(s);
pw.flush();
}
}
private class Server implements Runnable {
public void run() {
try {
while(start){
String str = br.readLine();
ta.setText(ta.getText()+str+'\n');
}
}catch (SocketException e){
System.out.println("退出了");
}catch (IOException e){
e.printStackTrace();
}
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;
public class QQServer {
ServerSocket ss;
List<Client> list = new ArrayList<Client>();
boolean start ;
public static void main(String[] args) {
new QQServer().receiveMethod();
}
public void receiveMethod(){
try {
ss = new ServerSocket(8980);
start = true;
}catch (BindException e){
System.out.println("端口已经被占用");
}catch (IOException e){
e.printStackTrace();
}
try {
while (start){
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client is connected");
new Thread(c).start();
list.add(c);
}
}catch(IOException e){
e.printStackTrace();
}finally {
try {
ss.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
private class Client implements Runnable {
BufferedReader br;
PrintWriter pw;
Socket socket;
boolean connect;
public void run() {
try {
while(connect){
String s = br.readLine();
System.out.println(s);
for (int i=0; i<list.size(); i++){
Client client = list.get(i);
client.send(s);
}
}
}catch (EOFException e){
System.out.println("客户端已经退出");
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if(br != null){
br.close();
}
if(pw != null){
pw.close();
}
if(socket != null){
socket.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
public void send(String s){
pw.println(s);
pw.flush();
}
public Client(Socket socket){
this.socket = socket;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
connect = true;
}catch (IOException e){
e.printStackTrace();
}
}
}
}
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class QQClient extends Frame {
Socket socket;
BufferedReader br;
PrintWriter pw;
boolean start;
TextField tf = new TextField();
TextArea ta = new TextArea();
public static void main(String[] args) {
new QQClient().launchFrame();
}
public void launchFrame(){
setBounds(400,300,300,300);
add(ta,BorderLayout.NORTH);
add(tf, BorderLayout.SOUTH);
tf.addActionListener(new Monitor());
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
setVisible(true);
try {
socket = new Socket("127.0.0.1", 8980);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
start = true;
System.out.println("kk");
}catch (UnknownHostException e){
System.out.println("如果无法确定主机的 IP 地址");
}catch (IOException e){
e.printStackTrace();
}
new Thread(new Server()).start();
}
public void disconnect(){
try {
br.close();
pw.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
}
private class Monitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s = tf.getText();
tf.setText("");
pw.println(s);
pw.flush();
}
}
private class Server implements Runnable {
public void run() {
try {
while(start){
String str = br.readLine();
ta.setText(ta.getText()+str+'\n');
}
}catch (SocketException e){
System.out.println("退出了");
}catch (IOException e){
e.printStackTrace();
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询