62,614
社区成员
发帖
与我相关
我的任务
分享
Socket client = server.accept();
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class TestClient2 {
public static void main(String[] args) throws UnknownHostException, IOException {
//创建Socket对象
Socket client = new Socket("localhost",9998);
DataOutputStream dos;
DataInputStream dis;
Scanner input = new Scanner(System.in);
while(true) {
//获取输出流
dos = new DataOutputStream(client.getOutputStream());
String str = input.next();
dos.writeUTF(str);
//获取输入流
dis = new DataInputStream(client.getInputStream());
System.out.println(dis.readUTF());
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2 {
public static void main(String[] args) throws IOException {
System.out.println("-------------服务器已启动---------------------");
ServerSocket server = new ServerSocket(9998);
DataInputStream dis;
DataOutputStream dos;
while(true) {
Socket client = server.accept();
dis = new DataInputStream(client.getInputStream());
String str = dis.readUTF();
System.out.println("客户端发动的数据为:"+str);
dos= new DataOutputStream(client.getOutputStream());
dos.writeUTF("服务器收到你的:"+str);
}
}
}
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
public class SocketDemo {
public static void main(String... args) throws IOException {
final Socket socket = new Socket("localhost", 8043);
new Thread() {
public void run() {
try {
InputStream is = socket.getInputStream();
pipe(is,System.out);
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
}
}.start();
new Thread() {
public void run() {
try {
OutputStream os = socket.getOutputStream();
PrintStream out = new PrintStream(os);
pipe(System.in, out);
out.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
}
}.start();
}
public static void pipe(InputStream is, PrintStream out) throws IOException {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null)
out.println(line);
out.flush();
}
}
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSocketDemo {
public static void main(String... args) throws IOException {
ServerSocket server = new ServerSocket(8043);
while (true) {
final Socket accept = server.accept();
new Thread() {
public void run() {
try {
InputStream is = accept.getInputStream();
SocketDemo.pipe(is, System.out);
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
accept.close();
} catch (IOException e) {
}
}
}
}.start();
new Thread() {
public void run() {
try {
OutputStream os = accept.getOutputStream();
PrintStream out = new PrintStream(os);
SocketDemo.pipe(System.in, out);
out.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
accept.close();
} catch (IOException e) {
}
}
}
}.start();
}
}
}