第1个回答 2013-04-08
客户端,包括向服务端发送信息的线程:
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上连自己试试
第2个回答 2011-05-25
商品社会拒绝白干,第一,没有人会给你这个程序。第二,就算有人给你,他也不会耐心告诉你怎么配置,怎么装数据库。怎么运行。 随随便便来这里就想白要,你觉得可能吗。我说话可能不好听,但是这是事实。本回答被网友采纳
第3个回答 2011-05-26
我擦,这怎么做?你Java都没学,就像做东西啊.下个JDK,设置下环境变量。
第4个回答 2011-05-25
不可能的孩纸···都要数据库的,要配置的,木有能直接运行的···
第5个回答 2011-05-25
我擦,这怎么做?你Java都没学,就像做东西啊.下个JDK,设置下环境变量。