java Socket通信,客户端实现好友列表

java Socket通信,客户端实现好友列表,第1张

问题一:客户端接收可以采用下列步骤:

1、无论客户端还是服务器都要有能力构造实体Bean(比如叫做UserBean,存放用户信息),构造UserList封装UserBean数组。并且,上述类要支持序列化和反序列化。

2、服务端将List<UserBean>UserList序列化,然后利用ServerSocket发送。

3、客户端利用Socket接收,对UserList反序列化,遍历UserBean数组得到每一个用户的信息。

问题二:对于Socket,不存在得到还是得不到的——

你要利用Tcp、Udp协议先要构造和初始化Socket才行。

客户端Socket和服务端Socket进行全双工通信。

即使是最简单IM演示程序,用户的好友列表都应该考虑在服务器端持久化和管理。

客户端程序要想得到其好友列表,只需要执行一次”请求/响应”即可。写这方面的程序,可以参考XMPP相关技术。

有好友列表就是客户端赛, 客户端和服务器之间就一个socket连接(通常情况下)

你这样也可以, 反正只有一个socket, 你可以直接public static Socket 一个就行,随便哪里都可以拿到socket. 反正怎么简单怎么来,

客户端:

package chatroom

import java.awt.*

import java.awt.event.*

import java.net.*

import java.io.*

import javax.swing.*

public class client extends JFrame implements ActionListener,Runnable{

JPanel conn,text,send

JLabel name,sendto

JComboBox list

JButton con,snd,clear

JTextArea talk

JTextField who,say

JScrollPane jsp

Socket client

InputStream is

OutputStream os

PrintStream ps

BufferedReader br

String receive,yousay

Thread th

DataInputStream dis

Double tof

client()

{

super("聊天室客户端")

this.setSize(800,600)

this.setResizable(false)

conn=new JPanel()

text=new JPanel()

send=new JPanel()

this.getContentPane().add(conn)

conn.setBounds(0, 0, this.getWidth(),50)

name=new JLabel("姓名:")

who=new JTextField()

con=new JButton("连接")

conn.setLayout(null)

conn.add(name)

name.setBounds(30, 10, 50, 25)

conn.add(who)

who.setBounds(80, 10, 150, 25)

conn.add(con)

con.setBounds(250,10, 60, 25)

this.getContentPane().add(text)

text.setBounds(0,50,this.getWidth(),450)

text.setLayout(new BorderLayout())

jsp=new JScrollPane()

talk=new JTextArea()

jsp.getViewport().setView(talk)

text.add(jsp,"Center")

talk.setLineWrap(true)

this.getContentPane().add(send)

send.setLayout(null)

send.setBounds(0, 480, this.getWidth(), 150)

sendto=new JLabel("发送到:")

snd=new JButton("发送")

list=new JComboBox()

say=new JTextField()

clear=new JButton("清空")

send.add(sendto)

sendto.setBounds(30, 525, 50, 25)

send.add(list)

list.setBounds(100, 525, 75, 25)

send.add(say)

say.setEditable(true)

say.setBounds(200, 525, 300, 25)

send.add(snd)

snd.setBounds(520, 525, 100, 25)

send.add(clear)

clear.setBounds(650, 525, 100, 25)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

this.setVisible(true)

con.addActionListener(this)

snd.addActionListener(this)

}

public void actionPerformed(ActionEvent ae) {

if (ae.getActionCommand().equals("连接"))

{

try

{

client=new Socket(InetAddress.getLocalHost(),55555)

talk.append("连接成功...\n")

con.setText("断开")

is=client.getInputStream()

os=client.getOutputStream()

th=new Thread(this)

String id=who.getText()

byte b[]=id.getBytes()

DataOutputStream dos=new DataOutputStream(os)

int len=b.length

dos.writeInt(len)

dos.write(b)

th.start()

}catch(Exception e){talk.append("连接失败\n"+e.toString()+"0000")}

}

else if(ae.getSource()==snd)

{

if(list.getSelectedItem().toString()=="所有人")

{

yousay="@open"+say.getText()

byte b[]=yousay.getBytes()

DataOutputStream dos=new DataOutputStream(os)

try{

int len=b.length

dos.writeInt(len)

dos.write(b)

}catch(Exception e)

{

System.out.print(e.toString()+"1111")

}

}

else

{

yousay="@whisper"+say.getText()

byte b[]=yousay.getBytes()

byte w[]=list.getSelectedItem().toString().getBytes()

DataOutputStream dos=new DataOutputStream(os)

try{

int len=b.length

int wlen=w.length

dos.writeInt(len)//内容

dos.write(b)

dos.writeInt(wlen)//发送对象

dos.write(w)

}catch(Exception e)

{

System.out.print(e.toString()+"AAAA")

}

}

}

else if(ae.getActionCommand().equals("断开"))

{

try

{

client.close()

talk.append("连接已断开\n")

con.setText("连接")

}catch(Exception e){System.out.println(e.toString()+"2222")}

}

}

public void run()

{

while(true)

{

try

{

dis=new DataInputStream(is)

tof=dis.readDouble()

if(tof==1.1)

{

dis=new DataInputStream(is)

int number

list.removeAllItems()

list.addItem("所有人")

number=dis.readInt()

for(int i=0i<numberi++)

{

int len=dis.readInt()

byte b[]=new byte[len]

dis.read(b)

String name=new String(b)

list.addItem(name)

}

}

else if(tof==2.2)

{

try

{

dis=new DataInputStream(is)

int len=dis.readInt()

byte b[]=new byte[len]

dis.read(b)

receive=new String(b)

talk.append(receive+"\n")

}catch(Exception e){JOptionPane.showMessageDialog(this, "连接已断开","消息",JOptionPane.INFORMATION_MESSAGE)break}

}

}catch(Exception e){JOptionPane.showMessageDialog(this, "连接已断开","消息",JOptionPane.INFORMATION_MESSAGE)break}

}

}

public static void main(String[] args) {

client cl=new client()

}

}

服务端:

package chatroom

import java.awt.*

import javax.swing.*

import java.net.*

import java.util.Vector

import java.io.*

public class server extends JFrame implements Runnable{

JPanel jp1

JTextArea jta

JScrollPane jsp

Socket socket=null

ServerSocket server

InputStream is

OutputStream os

static int i=0,login

int no

static String s=""

Thread sr

Thread th[]=new Thread[20]

static Vector ol=new Vector()

static public byte w[]=null

static String from=""//人名

static String to=""

static String fromtext=""//内容

static String totext=""

server()

{

super("聊天室服务端")

this.getContentPane().setLayout(new GridLayout(1,1))

this.setSize(400,300)

jp1=new JPanel()

jta=new JTextArea()

jsp=new JScrollPane()

this.getContentPane().add(jp1)

jp1.setLayout(new GridLayout(1,1))

jsp.getViewport().setView(jta)

jp1.add(jsp)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

setVisible(true)

}

public static void main(String[] args) {

server sr=new server()

sr.sr=new Thread(sr)

sr.sr.start()

sendtoall sta=new sendtoall()

returnfrom rf=new returnfrom(sr)

returnto rt=new returnto(sr)

sta.start()

rf.start()

rt.start()

}

public void run()

{

try

{

server=new ServerSocket(55555)

while(true)

{

socket=server.accept()

is=socket.getInputStream()

DataInputStream dis=new DataInputStream(is)

int len=dis.readInt()

byte b[]=new byte[len]

dis.read(b)

String id=new String(b)

record v=new record(id,socket)

ol.addElement(v)

for (int i=0i<ol.size()i++)

{

if (ol.elementAt(i).equals(v))

{

no=i

}

}

login=1

s=id+"已来到聊天室!"

th[no]=new Thread(new receive(this,no))

th[no].start()

}

}catch(Exception e){System.out.println(e.toString()+"aaaa")}

}

}

class receive implements Runnable

{

InputStream is

OutputStream os

server sr

Socket sk

int i

String name

String ip

receive(server sr,int i)

{

this.sr=sr

this.i=i

sk=((record)sr.ol.elementAt(i)).ip

name=((record)sr.ol.elementAt(i)).name

ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString()

}

public void run()

{

while(true)

{

try

{

is=sk.getInputStream()

DataInputStream dis=new DataInputStream(is)

int len=dis.readInt()

byte b[]=new byte[len]

dis.read(b)

String abc=new String(b)

sr.jta.append(abc)

if(abc.substring(0,5).equals("@open"))

{

server.s=name+"["+ip.substring(1, ip.length())+"]"+"说:"+abc.substring(5,abc.length())

sr.jta.append(server.s+"\n")

}

else if(abc.substring(0,8).equals("@whisper"))

{

int wlen=dis.readInt()

sr.w=new byte[wlen]

dis.read(sr.w)

server.to=new String(sr.w)

server.from=((record)sr.ol.elementAt(i)).name

String ip=((record)sr.ol.elementAt(i)).ip.getInetAddress().toString()

// server.s=server.from+"对"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地说:"+abc.substring(8,abc.length())

server.fromtext="你对"+server.to+"["+ip.substring(1, ip.length())+"]"+"悄悄地说:"+abc.substring(8,abc.length())

server.totext=server.from+"["+ip.substring(1, ip.length())+"]"+"对你悄悄地说:"+abc.substring(8,abc.length())

sr.jta.append(server.s+"\n")

}

}catch(Exception e)

{

try

{

DataOutputStream dos=new DataOutputStream(os)

server.ol.removeElementAt(i)

server.s=name+"已离开聊天室."

server.login=1

break

}catch(Exception f){}

}

}

}

}

class sendtoall extends Thread

{

int len,number

byte b[]

server sr

Socket st

OutputStream os

DataOutputStream dos

public void run()

{

while(true)

{

try

{

if(server.login==1)

{

number=0

number=server.ol.size()

dos=new DataOutputStream(os)

for(int i=0i<server.ol.size()i++)

{

st=((record)sr.ol.elementAt(i)).ip

os=st.getOutputStream()

dos=new DataOutputStream(os)

dos.writeDouble(1.1)

dos.writeInt(number)

for (int j=0j<numberj++)

{

String name=((record)sr.ol.elementAt(j)).name

byte b[]=name.getBytes()

int len=b.length

dos.writeInt(len)

dos.write(b)

}

}

server.login=0

}

else if(!server.s.equals("")&&sr.ol.size()>0)

{

b=server.s.getBytes()//String类型中 汉字占一个字节,但是byte类型中,汉字占两个字节

len=b.length

//len=server.s.length()

//b=new byte[len]

//b=server.s.getBytes()

for(int i=0i<server.ol.size()i++)

{

st=((record)sr.ol.elementAt(i)).ip

os=st.getOutputStream()

DataOutputStream dos=new DataOutputStream(os)

dos.writeDouble(2.2)

dos.writeInt(len)

dos.write(b)

}

server.s=""

}

}catch(Exception e){System.out.println(e.toString()+"sdasd")}

}

}

}

class returnfrom extends Thread

{

int len,wlen

byte b[]

byte w[]

server sr

Socket st

OutputStream os

DataOutputStream dos

//String from="",to=""

returnfrom(server sr)

{

this.sr=sr

}

public void run()

{

while(true)

{

if(!server.fromtext.equals(""))

{

b=server.fromtext.getBytes()

len=b.length

sr.jta.append(sr.fromtext)

try

{

for(int i=0i<server.ol.size()i++)

{

if(((record)sr.ol.elementAt(i)).name.equals(server.from))

{

st=((record)sr.ol.elementAt(i)).ip

os=st.getOutputStream()

DataOutputStream dos=new DataOutputStream(os)

dos.writeDouble(2.2)

dos.writeInt(len)

dos.write(b)

}

}

}catch(Exception e){System.out.println(e.toString()+"wwww")}

server.fromtext=""

server.from=""

}

}

}

}

class returnto extends Thread

{

int len,wlen

byte b[]

byte w[]

server sr

Socket st

OutputStream os

DataOutputStream dos

//String from="",to=""

returnto (server sr)

{

this.sr=sr

}

public void run()

{

while(true)

{

if(!server.totext.equals(""))

{

w=server.totext.getBytes()

wlen=w.length

try

{

for(int i=0i<server.ol.size()i++)

{

if(((record)sr.ol.elementAt(i)).name.equals(server.to))

{

st=((record)sr.ol.elementAt(i)).ip

os=st.getOutputStream()

DataOutputStream dos=new DataOutputStream(os)

dos.writeDouble(2.2)

dos.writeInt(wlen)

dos.write(w)

}

}

}catch(Exception e){System.out.println(e.toString()+"wwww")}

server.totext=""

server.to=""

}

}

}

}

class record

{

String name

Socket ip

record(String id,Socket socket)

{

name=id

ip=socket

}

}

以前学习java时写的,是个在线聊天室只有群聊,私聊,上下线提示,搞不好里面还有错误- -其他功能自己看着加吧,你要的功能实在是很好很强大.


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

原文地址: http://outofmemory.cn/bake/11812261.html

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

发表评论

登录后才能评论

评论列表(0条)

保存