1个回答
展开全部
文件1:
package com.qq;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
public class Server {
public Server() {
try {
ServerSocket s = new ServerSocket(8888);
Socket ss = s.accept();
OutputStream out = ss.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
InputStream in = ss.getInputStream();
DataInputStream din = new DataInputStream(in);
System.out.print(din.readUTF() + "!");
dout.writeUTF("你已经连接到服务器" + "\t" + "你的地址:" + ss.getInetAddress()
+ "\t" + "你的链接端口:" + ss.getLocalPort() + "\n");
new ReadMessage(din).start();
new SendMessage(dout).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
}
}
// 接受客户端信息
class ReadMessage extends Thread {
private DataInputStream din;
public ReadMessage(DataInputStream din) {
this.din = din;
}
public void run() {
String str;
try {
while (true) {
str = din.readUTF();
System.out.println(new Date().toLocaleString() + "客户端说:" + str);
if (str.equals("bye")) {
System.out.println("客户端下线!");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 发出服务器信息
class SendMessage extends Thread {
private DataOutputStream dout;
public SendMessage(DataOutputStream dout) {
this.dout = dout;
}
public void run() {
InputStreamReader inr = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(inr);
String str;
try {
while (true) {
str = buf.readLine();
dout.writeUTF(str);
if (str.equals("bye")) {
System.out.println("服务器退出!");
System.exit(1);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
----------------------------------------
文件2:
package com.qq;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
public class Client {
public Client() {
try {
Socket s = new Socket("192.168.1.2", 8888);
InputStream in = s.getInputStream();
DataInputStream din = new DataInputStream(in);
OutputStream out = s.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
dout.writeUTF("服务器你好!我是客户端");
System.out.println(din.readUTF());
new Thread(new SenderMessage(dout)).start();
new Thread(new ReaderMessage(din)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client();
}
}
class ReaderMessage implements Runnable {
private DataInputStream din;
public ReaderMessage(DataInputStream din) {
this.din = din;
}
public void run() {
String str;
try {
while (true) {
str = din.readUTF();
System.out.println(new Date().toLocaleString() + "服务器说:" + str);
if (str.equals("bye")) {
System.out.println("服务器已经关闭,此程序自动退出!");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class SenderMessage implements Runnable {
private DataOutputStream dout;
public SenderMessage(DataOutputStream dout) {
this.dout = dout;
}
public void run() {
String str;
InputStreamReader inf = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(inf);
try {
while (true) {
str = buf.readLine();
dout.writeUTF(str);
if (str.equals("bye")) {
System.out.println("客户端自己退出!");
System.exit(1);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.qq;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
public class Server {
public Server() {
try {
ServerSocket s = new ServerSocket(8888);
Socket ss = s.accept();
OutputStream out = ss.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
InputStream in = ss.getInputStream();
DataInputStream din = new DataInputStream(in);
System.out.print(din.readUTF() + "!");
dout.writeUTF("你已经连接到服务器" + "\t" + "你的地址:" + ss.getInetAddress()
+ "\t" + "你的链接端口:" + ss.getLocalPort() + "\n");
new ReadMessage(din).start();
new SendMessage(dout).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
}
}
// 接受客户端信息
class ReadMessage extends Thread {
private DataInputStream din;
public ReadMessage(DataInputStream din) {
this.din = din;
}
public void run() {
String str;
try {
while (true) {
str = din.readUTF();
System.out.println(new Date().toLocaleString() + "客户端说:" + str);
if (str.equals("bye")) {
System.out.println("客户端下线!");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 发出服务器信息
class SendMessage extends Thread {
private DataOutputStream dout;
public SendMessage(DataOutputStream dout) {
this.dout = dout;
}
public void run() {
InputStreamReader inr = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(inr);
String str;
try {
while (true) {
str = buf.readLine();
dout.writeUTF(str);
if (str.equals("bye")) {
System.out.println("服务器退出!");
System.exit(1);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
----------------------------------------
文件2:
package com.qq;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
public class Client {
public Client() {
try {
Socket s = new Socket("192.168.1.2", 8888);
InputStream in = s.getInputStream();
DataInputStream din = new DataInputStream(in);
OutputStream out = s.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);
dout.writeUTF("服务器你好!我是客户端");
System.out.println(din.readUTF());
new Thread(new SenderMessage(dout)).start();
new Thread(new ReaderMessage(din)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client();
}
}
class ReaderMessage implements Runnable {
private DataInputStream din;
public ReaderMessage(DataInputStream din) {
this.din = din;
}
public void run() {
String str;
try {
while (true) {
str = din.readUTF();
System.out.println(new Date().toLocaleString() + "服务器说:" + str);
if (str.equals("bye")) {
System.out.println("服务器已经关闭,此程序自动退出!");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class SenderMessage implements Runnable {
private DataOutputStream dout;
public SenderMessage(DataOutputStream dout) {
this.dout = dout;
}
public void run() {
String str;
InputStreamReader inf = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(inf);
try {
while (true) {
str = buf.readLine();
dout.writeUTF(str);
if (str.equals("bye")) {
System.out.println("客户端自己退出!");
System.exit(1);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询