要JAVA聊天程序代码,急用

要JAVA聊天程序代码,急用,第1张

import java.io.InputStream

import java.io.DataInputStream

import java.io.InputStreamReader

import java.io.OutputStream

import java.io.DataOutputStream

import java.io.BufferedReader

import java.net.ServerSocket

import java.net.Socket

import java.io.IOException

import java.util.Date

class Server

{

public Server()

{

try

{

ServerSocket s=new ServerSocket(8888)

Socket ss=s.accept()

OutputStream out=ss.getOutputStream()

DataOutputStream dout=new DataOutputStream(out)

InputStream in=ss.getInputStream()

DataInputStream din=new DataInputStream(in)

System.out.print(din.readUTF()+"!")

dout.writeUTF("你已经连接到服务器"+"\t"+"你的地址:"+ss.getInetAddress()+"\t"

+"你的链接端口:"+ss.getLocalPort()+"\n")

new ReadMessage(din).start()

new SendMessage(dout).start()

}

catch (IOException e)

{

e.printStackTrace()

}

}

public static void main(String[] args)

{

new Server()

}

}

//接受客户端信息

class ReadMessage extends Thread

{

private DataInputStream din

public ReadMessage(DataInputStream din)

{

this.din=din

}

public void run()

{

String str

try

{

while (true)

{

str=din.readUTF()

System.out.println(new Date().toLocaleString()+"客户端说:"+str)

if (str.equals("bye"))

{

System.out.println("客户端下线!")

break

}

}

}

catch (IOException e)

{

e.printStackTrace()

}

}

}

// 发出服务器信息

class SendMessage extends Thread

{

private DataOutputStream dout

public SendMessage(DataOutputStream dout)

{

this.dout=dout

}

public void run()

{

InputStreamReader inr=new InputStreamReader(System.in)

BufferedReader buf=new BufferedReader(inr)

String str

try

{

while(true)

{

str=buf.readLine()

dout.writeUTF(str)

if (str.equals("bye"))

{

System.out.println("服务器退出!")

System.exit(1)

}

}

}

catch (IOException e)

{

e.printStackTrace()

}

}

}

import java.io.InputStream

import java.io.DataInputStream

import java.io.InputStreamReader

import java.io.OutputStream

import java.io.DataOutputStream

import java.io.BufferedReader

import java.net.Socket

import java.io.IOException

import java.util.Date

class Client

{

public Client()

{

try

{

Socket s=new Socket("192.168.1.2",8888)

InputStream in=s.getInputStream()

DataInputStream din=new DataInputStream(in)

OutputStream out=s.getOutputStream()

DataOutputStream dout=new DataOutputStream(out)

dout.writeUTF("服务器你好!我是客户端")

System.out.println(din.readUTF())

new Thread(new SenderMessage(dout)).start()

new Thread(new ReaderMessage(din)).start()

}

catch (IOException e)

{

e.printStackTrace()

}

}

public static void main(String[] args)

{

new Client()

}

}

class ReaderMessage implements Runnable

{

private DataInputStream din

public ReaderMessage(DataInputStream din)

{

this.din=din

}

public void run()

{

String str

try

{

while(true)

{

str=din.readUTF()

System.out.println(new Date().toLocaleString()+"服务器说:"+str)

if (str.equals("bye"))

{

System.out.println("服务器已经关闭,此程序自动退出!")

break

}

}

}

catch (IOException e)

{

e.printStackTrace()

}

}

}

class SenderMessage implements Runnable

{

private DataOutputStream dout

public SenderMessage(DataOutputStream dout)

{

this.dout=dout

}

public void run()

{

String str

InputStreamReader inf=new InputStreamReader(System.in)

BufferedReader buf=new BufferedReader(inf)

try

{

while (true)

{

str=buf.readLine()

dout.writeUTF(str)

if (str.equals("bye"))

{

System.out.println("客户端自己退出!")

System.exit(1)

}

}

}

catch (IOException e)

{

e.printStackTrace()

}

}

}

【ClientSocketDemo.java 客户端Java源代码】

import java.net.*

import java.io.*

public class ClientSocketDemo

{

//声明客户端Socket对象socket

Socket socket = null

//声明客户器端数据输入输出流

DataInputStream in

DataOutputStream out

//声明字符串数组对象response,用于存储从服务器接收到的信息

String response[]

//执行过程中,没有参数时的构造方法,本地服务器在本地,取默认端口10745

public ClientSocketDemo()

{

try

{

//创建客户端socket,服务器地址取本地,端口号为10745

socket = new Socket("localhost",10745)

//创建客户端数据输入输出流,用于对服务器端发送或接收数据

in = new DataInputStream(socket.getInputStream())

out = new DataOutputStream(socket.getOutputStream())

//获取客户端地址及端口号

String ip = String.valueOf(socket.getLocalAddress())

String port = String.valueOf(socket.getLocalPort())

//向服务器发送数据

out.writeUTF("Hello Server.This connection is from client.")

out.writeUTF(ip)

out.writeUTF(port)

//从服务器接收数据

response = new String[3]

for (int i = 0i <response.lengthi++)

{

response[i] = in.readUTF()

System.out.println(response[i])

}

}

catch(UnknownHostException e){e.printStackTrace()}

catch(IOException e){e.printStackTrace()}

}

//执行过程中,有一个参数时的构造方法,参数指定服务器地址,取默认端口10745

public ClientSocketDemo(String hostname)

{

try

{

//创建客户端socket,hostname参数指定服务器地址,端口号为10745

socket = new Socket(hostname,10745)

in = new DataInputStream(socket.getInputStream())

out = new DataOutputStream(socket.getOutputStream())

String ip = String.valueOf(socket.getLocalAddress())

String port = String.valueOf(socket.getLocalPort())

out.writeUTF("Hello Server.This connection is from client.")

out.writeUTF(ip)

out.writeUTF(port)

response = new String[3]

for (int i = 0i <response.lengthi++)

{

response[i] = in.readUTF()

System.out.println(response[i])

}

}

catch(UnknownHostException e){e.printStackTrace()}

catch(IOException e){e.printStackTrace()}

}

//执行过程中,有两个个参数时的构造方法,第一个参数hostname指定服务器地址

//第一个参数serverPort指定服务器端口号

public ClientSocketDemo(String hostname,String serverPort)

{

try

{

socket = new Socket(hostname,Integer.parseInt(serverPort))

in = new DataInputStream(socket.getInputStream())

out = new DataOutputStream(socket.getOutputStream())

String ip = String.valueOf(socket.getLocalAddress())

String port = String.valueOf(socket.getLocalPort())

out.writeUTF("Hello Server.This connection is from client.")

out.writeUTF(ip)

out.writeUTF(port)

response = new String[3]

for (int i = 0i <response.lengthi++)

{

response[i] = in.readUTF()

System.out.println(response[i])

}

}

catch(UnknownHostException e){e.printStackTrace()}

catch(IOException e){e.printStackTrace()}

}

public static void main(String[] args)

{

String comd[] = args

if(comd.length == 0)

{

System.out.println("Use localhost(127.0.0.1) and default port")

ClientSocketDemo demo = new ClientSocketDemo()

}

else if(comd.length == 1)

{

System.out.println("Use default port")

ClientSocketDemo demo = new ClientSocketDemo(args[0])

}

else if(comd.length == 2)

{

System.out.println("Hostname and port are named by user")

ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1])

}

else System.out.println("ERROR")

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

【ServerSocketDemo.java 服务器端Java源代码】

import java.net.*

import java.io.*

public class ServerSocketDemo

{

//声明ServerSocket类对象

ServerSocket serverSocket

//声明并初始化服务器端监听端口号常量

public static final int PORT = 10745

//声明服务器端数据输入输出流

DataInputStream in

DataOutputStream out

//声明InetAddress类对象ip,用于获取服务器地址及端口号等信息

InetAddress ip = null

//声明字符串数组对象request,用于存储从客户端发送来的信息

String request[]

public ServerSocketDemo()

{

request = new String[3]//初始化字符串数组

try

{

//获取本地服务器地址信息

ip = InetAddress.getLocalHost()

//以PORT为服务端口号,创建serverSocket对象以监听该端口上的连接

serverSocket = new ServerSocket(PORT)

//创建Socket类的对象socket,用于保存连接到服务器的客户端socket对象

Socket socket = serverSocket.accept()

System.out.println("This is server:"+String.valueOf(ip)+PORT)

//创建服务器端数据输入输出流,用于对客户端接收或发送数据

in = new DataInputStream(socket.getInputStream())

out = new DataOutputStream(socket.getOutputStream())

//接收客户端发送来的数据信息,并显示

request[0] = in.readUTF()

request[1] = in.readUTF()

request[2] = in.readUTF()

System.out.println("Received messages form client is:")

System.out.println(request[0])

System.out.println(request[1])

System.out.println(request[2])

//向客户端发送数据

out.writeUTF("Hello client!")

out.writeUTF("Your ip is:"+request[1])

out.writeUTF("Your port is:"+request[2])

}

catch(IOException e){e.printStackTrace()}

}

public static void main(String[] args)

{

ServerSocketDemo demo = new ServerSocketDemo()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11855200.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存