我要一份用java网络编程写的点对点的两人聊天程序(TCP和UDP)?

我要一份用java网络编程写的点对点的两人聊天程序(TCP和UDP)?,第1张

Server端:

import java.io.*

import java.net.*

import java.applet.Applet

public class TalkServer{

public static void main(String args[]) {

try{

ServerSocket server=null

try{

server=new ServerSocket(4700)

}catch(Exception e) {

System.out.println("can not listen to:"+e)

}

Socket socket=null

try{

socket=server.accept()

}catch(Exception e) {

System.out.println("Error."毕则+e)

}

String line

BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()))

PrintWriter os=new PrintWriter(socket.getOutputStream())

BufferedReader sin=new BufferedReader(new InputStreamReader(System.in))

System.out.println("Client:"+is.readLine())

line=sin.readLine()

while(!line.equals("bye")){

os.println(line)

os.flush()

System.out.println("Server:"+line)

System.out.println("Client:"+is.readLine())

line=sin.readLine()

}

os.close()

is.close()

socket.close()

server.close()

}catch(Exception e){

System.out.println("Error:"+e)

}

}

}

Client端:

import java.io.*

import java.net.*

public class TalkClient {

public static void main(String args[]) {

try{

Socket socket=new Socket("127.0.0.1",4700)

BufferedReader sin=new BufferedReader(new InputStreamReader(System.in))

PrintWriter os=new PrintWriter(socket.getOutputStream())

BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()))

String readline

readline=sin.readLine()//从系统标准输入读入稿迅一字符串

while(!readline.equals("bye")){

os.println(readline)

os.flush()

System.out.println("Client:"+readline)

System.out.println("Server:"+is.readLine())

readline=sin.readLine()//从系统标准输入读入一字符串

}

os.close()//关闭Socket输出流键数此

is.close()//关闭Socket输入流

socket.close()//关闭Socket

}catch(Exception e) {

System.out.println("Error"+e)//出错,则打印出错信息

}

}

}

要实现Java一对一聊天室,可以使用Socket编程,以下是一个简单的实现步骤:

1.创建客户端芹埋轮和服务器端的Socket对象,分别用于连接客户端和服务器端;

2.在客户端和服务器端中分别创建输入和输出流,液慎用于发送和接收消息

3.实现客户端和服务器端的消息传递机制,即客户端和服务器端可以相互发送和接收消息;

4.实现GUI,即为客户端创建一个嫌信用户界面,使得用户可以方便地输入和接收消息;

5.在代码中添加异常处理机制,以处理可能发生的异常情况。

这个时比较简单的山薯哪。

package QQ

import java.net.*

import java.io.*

import java.sql.*

import java.util.*

public class QQServer {

public static void main(String args[]) {

try {

HashMap<String , Socket>hm = new

HashMap<String , Socket>()

// 服务器到8000端口监听(1)

ServerSocket ss = new ServerSocket

(8000)

while (true) {

System.out.println("服手败务器正在

8000端口监听.....")

Socket s = ss.accept()

MyService t = new MyService()

t.setSocket(s)

t.setHashMap(hm)

t.start()

}

} catch (Exception e) {

e.printStackTrace()

}

}

}

class MyService extends Thread {

private Socket s

private HashMap<String , Socket>hm

public void setHashMap(HashMap<String , Socket>hm){

this.hm = hm

}

public void setSocket(Socket s){

this.s = s

}

public void run() {

try {

/逗码/ 接收客户端发送来的用户名和密码(2)

InputStream is = s.getInputStream()

InputStreamReader isr = new InputStreamReader

(is)

BufferedReader br = new BufferedReader(isr)

String uandp = br.readLine()

// 拆分用户名和密码(4)

String u = ""

String p = ""

try{

u = uandp.split("%")[0]

p = uandp.split("%")[1]

}catch(Exception e){}

// 到数据库中验证(5)

Class.forName

("com.microsoft.jdbc.sqlserver.SQLServerDriver")

Connection cn = DriverManager

.getConnection(

"jdbc:microsoft:sqlserver://127.0.0.1:1433databasename=qq2",

"sa", "123")

PreparedStatement ps = cn

.prepareStatement("select

username from username where username=? and password=?")

ps.setString(1, u)

ps.setString(2, p)

ResultSet rs = ps.executeQuery()

// 发送确认信息到客户端(7)

OutputStream os = s.getOutputStream()

OutputStreamWriter osw = new

OutputStreamWriter(os)

PrintWriter pw = new PrintWriter(osw, true)

if (rs.next()) {

pw.println("ok")

//将自己的名字发送给HashMap中的其他人

(13)

for(Socket ts : hm.values()){

OutputStream tos =

ts.getOutputStream()

OutputStreamWriter tosw = new

OutputStreamWriter(tos)

PrintWriter tpw = new

PrintWriter(tosw , true)

tpw.println("user%"+u)

}

//将其他人的名字发送给自己(13)

for(String tu : hm.keySet()){

pw.println("user%"+tu)

}

//将用户名和对应的Socket存入HashMap

(13)

hm.put(u, s)

// 接收客户端发送来的信息(11)

while (true) {

String message = br.readLine

()

if(message.equals("exit")){

for(Socket ts :

hm.values()){

OutputStream

tos = ts.getOutputStream()

OutputStreamWriter tosw = new OutputStreamWriter(tos)

PrintWriter

tpw = new PrintWriter(tosw , true)

tpw.println

("exit%"+u)

}

hm.remove(u)

}

String to = message.split

("%")[0]

String mess = message.split

("%")[1]

Socket ts = hm.get(to)

OutputStream tos =

ts.getOutputStream()

OutputStreamWriter tosw = new

OutputStreamWriter(tos)

PrintWriter tpw = new

PrintWriter(tosw, true)

tpw.println("mess%"+mess

+"\n")

System.out.println(message)

}

} else {

pw.println("err")

}

} catch (Exception e) {}

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存