现在谁有时间帮你写这个?
我觉得还是自己想理解吧
以http形式,主要的就是服务器端有一个资源池来存储所有的未发送消息记录;客户端不断进行连接。
以socket形式,网络上有很多教程
我觉得还是自己想理解吧
以http形式,主要的就是服务器端有一个资源池来存储所有的未发送消息记录;客户端不断进行连接。
以socket形式,网络上有很多教程
解决方案 »
- 100分求解:构造器模式中常用的那个静态内部类
- 用了spring or struts 有什么好处?
- 快绝望了:UDP接受存入LinkedList突然出现奇怪的问题
- 定义final int i = new int[2];之后树组维数无法改变,但内容可变,如何使内容也不可变
- 关于图像加载的问题,请指教(在线等)
- j2se界面设计中类的设计问题
- 编译到dataModule11.queryDataSet.setByte("OrdNum", 1);报错!
- 怎么实现Socket的异步通讯??谢谢
- javaw.exe是干什么用的呀?
- 如何用java实现excel表格的加密
- 请问如何配置J2EE这方面我比较菜,有什么好书请指教一下!
- 各位大虾:如何编程更改本机的ip地址?
看看这个 昨天没看到吗
共同学习java!!
* @Server.java
* @version 1.0
*/import java.io.*;
import java.net.*;
import java.awt.*;public class Server
extends Frame
implements Runnable {
ServerSocket server;
Socket connection;
OutputStream output;
InputStream input; Thread outThread; TextArea display;
TextField text1;
Button startButton; public static void main(String args[]) {
Server s = new Server();
} public Server() {
super("Server");
startButton = new Button("Start the server");
text1 = new TextField(20);
display = new TextArea(7, 30);
display.setEditable(false);
add("North", startButton);
add("Center", display);
add("South", text1); resize(300, 600);
show();
} public boolean action(Event e, Object o) {
if (e.target == startButton) {
display.setText("启动服务器...\n");
startButton.setEnabled(false);
try {
server = new ServerSocket(5000, 100);
connection = server.accept();
output = connection.getOutputStream();
input = connection.getInputStream(); outThread = new Thread(this);
outThread.start();
}
catch (IOException ee) {}
}
else if (e.target == text1) {
byte writeBytes[] = new byte[50];
String s = "Server: " + text1.getText() + "\n";
text1.setText("");
writeBytes = s.getBytes();
display.append(s);
try {
output.write(writeBytes);
}
catch (IOException ee) {}
if (s.trim().equals("Server: exit")) {
outThread.stop();
quit();
}
}
return true;
} public void run() {
while (true) {
byte readBytes[] = new byte[50];
try {
input.read(readBytes);
}
catch (IOException e) {}
String s = new String(readBytes);
display.append(s);
if (s.trim().equals("Client: exit"))
break;
}
quit();
} public void quit() {
try {
output.close();
input.close();
connection.close();
}
catch (IOException e) {}
startButton.setEnabled(true);
} public boolean handleEvent(Event e) {
if (e.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(e);
}
}
* @Client.java
* @author not attributable
* @version 1.0
*/
import java.io.*;
import java.net.*;
import java.awt.*;public class Client
extends Frame
implements Runnable {
Socket client;
OutputStream output;
InputStream input; Thread outThread; TextArea display;
TextField text1;
Button startButton; public static void main(String args[]) {
Client c = new Client();
} public Client() {
super("Client");
startButton = new Button("Connect to server");
text1 = new TextField(20);
display = new TextArea(7, 30);
display.setEditable(false);
add("North", startButton);
add("Center", display);
add("South", text1); resize(300, 600);
show();
} public boolean action(Event e, Object o) {
if (e.target == startButton) {
display.setText("连接服务器...\n");
startButton.setEnabled(false);
try {
client = new Socket("127.0.0.1", 5000); output = client.getOutputStream();
input = client.getInputStream(); outThread = new Thread(this);
outThread.start();
}
catch (IOException ee) {}
}
else if (e.target == text1) {
byte writeBytes[] = new byte[50];
String s = "Client: " + text1.getText() + "\n";
text1.setText("");
writeBytes = s.getBytes();
display.append(s);
try {
output.write(writeBytes);
}
catch (IOException ee) {}
if (s.trim().equals("Client: exit")) {
outThread.stop();
quit();
}
}
return true;
} public void run() {
while (true) {
byte readBytes[] = new byte[50];
try {
input.read(readBytes);
}
catch (IOException e) {}
String s = new String(readBytes);
display.append(s);
if (s.trim().equals("Server: exit"))
break;
}
quit();
} public void quit() {
try {
output.close();
input.close();
client.close(); }
catch (IOException e) {}
startButton.setEnabled(true);
} public boolean handleEvent(Event e) {
if (e.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(e);
}
}
哪位大哥能提供点比较实用的源码
不要太复杂了,只要能实现发送接收文本和聊天记录三个基本功能就行了
本人的程序与楼主的要求比较符合,实现简单的局域网中发送和接收文本,已经调试成功,并且已经发到楼主的邮箱中,现在贴出来供大家参考:import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.GregorianCalendar;
public class QQ extends Frame implements ActionListener
{
Label l1=new Label("请输入您要发送的信息(限英文):");
Label l2=new Label("以下是你收到的消息记录:");
Label l3=new Label("把以上消息发给如下IP地址:");
TextArea input=new TextArea("",7,14,TextArea.SCROLLBARS_BOTH);
TextArea output=new TextArea("",8,14,TextArea.SCROLLBARS_BOTH);
TextField IPAdd=new TextField("127.0.0.1");
Button send=new Button("发送消息");
Button about=new Button("关于");
Button clear=new Button("清空消息纪录");
GregorianCalendar time=new GregorianCalendar();
QQ()
{
super("仿QQ聊天工具");
setLayout(null);
setLocation(250, 250);
this.setSize(518, 218);
this.setResizable(false);
this.setBackground(new Color(220, 220, 220));
Toolkit kit=Toolkit.getDefaultToolkit();
Image myImage=kit.getImage("QQ.bmp");
this.setIconImage(myImage); l1.setFont(new Font("宋体",Font.PLAIN,12));
l1.setForeground(new Color(0, 0, 192));
l1.setBounds(8,28,216, 16); input.setBackground(new Color(255, 255, 128));
input.setFont(new Font("Times New Roman",Font.BOLD,15));
input.setForeground(Color.magenta);
input.setBounds(8,44,248, 120); output.setBackground(new Color(128, 255, 255));
output.setFont(new Font("Times New Roman",Font.PLAIN,12));
output.setForeground(new Color(192, 64, 0));
output.setBounds(264, 44,248, 136);
output.setEditable(false); send.setFont(new Font("新宋体",Font.PLAIN,12));
send.setLocation(136, 188);
send.setSize(120, 22); clear.setFont(new Font("新宋体",Font.PLAIN,12));
clear.setLocation(392, 188);
clear.setSize(120, 22); l2.setFont(new Font("宋体",Font.PLAIN, 12));
l2.setForeground(new Color(0, 0, 192));
l2.setBounds(264,28,216, 16); about.setFont(new Font("新宋体",Font.PLAIN,12));
about.setLocation(264, 188);
about.setSize(120, 22); l3.setFont(new Font("宋体",Font.PLAIN,12));
l3.setForeground(new Color(0, 0, 192));
l3.setBounds(8,172,160,16); IPAdd.setFont(new Font("新宋体",Font.PLAIN,12));
IPAdd.setLocation(8, 190);
IPAdd.setSize(120, 19); add(l1);add(input);add(l3);add(l2);add(output);
add(IPAdd);add(send);add(about);add(clear);
addWindowListener(new closeWin());
send.addActionListener(this);
about.addActionListener(this);
clear.addActionListener(this); show();
waitForData();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==send)
sendData();
else
if(e.getSource()==clear)
output.setText("");
else
if(e.getSource()==about)
{
//QQ test=new QQ();
//test.show();
}
}
public static void main(String args[])
{
new QQ();
}
void sendData()
{
try{
String msg=input.getText();
if(msg.equals(""))
return;
input.setText("");
String ad=IPAdd.getText();
InetAddress tea=InetAddress.getLocalHost();
String asd=tea.getHostAddress();//发送方的IP地址
output.append("["+asd+"]:("+time.get(GregorianCalendar.YEAR)+"-"+time.get(GregorianCalendar.MONTH)+"-"+time.get(GregorianCalendar.DATE)+" "+time.get(GregorianCalendar.HOUR)+":"+time.get(GregorianCalendar.MINUTE)+":"+time.get(GregorianCalendar.SECOND)+") "+"\n"+msg+"\n");
msg="From ["+asd+"]:("+time.get(GregorianCalendar.YEAR)+"-"+time.get(GregorianCalendar.MONTH)+"-"+time.get(GregorianCalendar.DATE)+" "+time.get(GregorianCalendar.HOUR)+":"+time.get(GregorianCalendar.MINUTE)+":"+time.get(GregorianCalendar.SECOND)+") \n"+msg;
InetAddress address=InetAddress.getByName(ad);
int len=msg.length();
byte[] message=new byte[len];
msg.getBytes(0,len,message,0);
//哈夫满处理
DatagramPacket packet=new DatagramPacket(message,len,address,9999);
DatagramSocket socket=new DatagramSocket();
socket.send(packet);
}
catch(Exception e){}
}
void waitForData()
{
try{
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
DatagramSocket socket=new DatagramSocket(9999);
while(true)
{
socket.receive(packet);
String s=new String(buffer,0,0,packet.getLength());
//解吗处理
output.append(s+"\n");
packet=new DatagramPacket(buffer,buffer.length);
}
}
catch(Exception e){}
}
}
class closeWin extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Frame fr=(Frame)(e.getSource());
fr.dispose();
System.exit(0);
}
}