求一份基于JAVA的即时通讯软件的源码,必须要完整的放在eclipse下可直接运行的,最好不要有错误,简单就行 10
5个回答
展开全部
客户端,包括向服务端发送信息的线程:
package javase2.day06;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class MyClient {
public static void main(String[] args)throws Exception {
Socket socket = new Socket("localhost",8088);
OutputStream out = socket.getOutputStream();
OutputStreamWriter writer =
new OutputStreamWriter(out,"utf-8");
PrintWriter pw = new PrintWriter(writer);
InputStream in = socket.getInputStream();
ReadThreadForClient thread =
new ReadThreadForClient(in);
thread.start();
Scanner scanner = new Scanner(System.in);
while(true){
pw.println(scanner.nextLine());
pw.flush();
}
socket.close();
}
}
服务端,包括接受来自客户端信息的线程:
package javase2.day06;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) throws Exception {
System.out.println("create server...");
ServerSocket ss = new ServerSocket(8088);
System.out.println("server created successful...");
System.out.println("waitting for a connection of client...");
Socket socket = ss.accept();
System.out.println("connect succeful!" + socket.getInetAddress());
InputStream is = socket.getInputStream();
InputStreamReader reader =
new InputStreamReader(is,"utf-8");
BufferedReader br =
new BufferedReader(reader);
OutputStream os = socket.getOutputStream();
WriteThreadForServer thread =
new WriteThreadForServer(os);
thread.start();
String info;
while(true){
info = br.readLine();
System.out.println("from client:" + info);
}
socket.close();
}
}
客户端接收服务端信息的线程:
package javase2.day06;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ReadThreadForClient extends Thread {
private InputStream in;
public ReadThreadForClient(InputStream in) {
this.in = in;
}
public void run() {
try {
InputStreamReader reader = new InputStreamReader(in, "utf-8");
BufferedReader br = new BufferedReader(reader);
String info;
while (true) {
try {
info = br.readLine();
System.out.println("server:" + info);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务端向客户端发送信息的线程:
package javase2.day06;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class WriteThreadForServer extends Thread {
private OutputStream out;
public WriteThreadForServer(OutputStream out) {
this.out = out;
}
public void run() {
try {
Scanner scanner = new Scanner(System.in);
OutputStreamWriter writer =
new OutputStreamWriter(out,"utf-8");
PrintWriter pw = new PrintWriter(writer);
while (true) {
pw.println(scanner.nextLine());
pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
其实一对一的通讯也不用区分客户端和服务端
这个程序简单到都有些简陋了 凑合看吧 不过java最基本的通讯也就是这样
可以在eclipse上连自己试试
package javase2.day06;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class MyClient {
public static void main(String[] args)throws Exception {
Socket socket = new Socket("localhost",8088);
OutputStream out = socket.getOutputStream();
OutputStreamWriter writer =
new OutputStreamWriter(out,"utf-8");
PrintWriter pw = new PrintWriter(writer);
InputStream in = socket.getInputStream();
ReadThreadForClient thread =
new ReadThreadForClient(in);
thread.start();
Scanner scanner = new Scanner(System.in);
while(true){
pw.println(scanner.nextLine());
pw.flush();
}
socket.close();
}
}
服务端,包括接受来自客户端信息的线程:
package javase2.day06;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) throws Exception {
System.out.println("create server...");
ServerSocket ss = new ServerSocket(8088);
System.out.println("server created successful...");
System.out.println("waitting for a connection of client...");
Socket socket = ss.accept();
System.out.println("connect succeful!" + socket.getInetAddress());
InputStream is = socket.getInputStream();
InputStreamReader reader =
new InputStreamReader(is,"utf-8");
BufferedReader br =
new BufferedReader(reader);
OutputStream os = socket.getOutputStream();
WriteThreadForServer thread =
new WriteThreadForServer(os);
thread.start();
String info;
while(true){
info = br.readLine();
System.out.println("from client:" + info);
}
socket.close();
}
}
客户端接收服务端信息的线程:
package javase2.day06;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ReadThreadForClient extends Thread {
private InputStream in;
public ReadThreadForClient(InputStream in) {
this.in = in;
}
public void run() {
try {
InputStreamReader reader = new InputStreamReader(in, "utf-8");
BufferedReader br = new BufferedReader(reader);
String info;
while (true) {
try {
info = br.readLine();
System.out.println("server:" + info);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务端向客户端发送信息的线程:
package javase2.day06;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class WriteThreadForServer extends Thread {
private OutputStream out;
public WriteThreadForServer(OutputStream out) {
this.out = out;
}
public void run() {
try {
Scanner scanner = new Scanner(System.in);
OutputStreamWriter writer =
new OutputStreamWriter(out,"utf-8");
PrintWriter pw = new PrintWriter(writer);
while (true) {
pw.println(scanner.nextLine());
pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
其实一对一的通讯也不用区分客户端和服务端
这个程序简单到都有些简陋了 凑合看吧 不过java最基本的通讯也就是这样
可以在eclipse上连自己试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-05-25
展开全部
商品社会拒绝白干,第一,没有人会给你这个程序。第二,就算有人给你,他也不会耐心告诉你怎么配置,怎么装数据库。怎么运行。 随随便便来这里就想白要,你觉得可能吗。我说话可能不好听,但是这是事实。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我擦,这怎么做?你Java都没学,就像做东西啊.下个JDK,设置下环境变量。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不可能的孩纸···都要数据库的,要配置的,木有能直接运行的···
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我擦,这怎么做?你Java都没学,就像做东西啊.下个JDK,设置下环境变量。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询