2个回答
展开全部
//客户端
public class Client {
public static void main(String[] args) {
new Client().ClientTest();
}
public void ClientTest() {
try {
Socket sk = new Socket("127.0.0.1", 1234); // 链接服务器(链接ip为127.0.0.1,端口为1234)
while (true) {
InputStreamReader isr2 = new InputStreamReader(System.in);
BufferedReader bdr = new BufferedReader(isr2);
String bd = bdr.readLine();// 等待输入
PrintWriter pw = new PrintWriter(sk.getOutputStream(), true);// 链接成功的话。可以给服务器发送数据
// .我们通过pw发数据。true即使刷新
pw.println(bd);// 服务器响应的数据
// 接受服务器响应的数据
InputStreamReader isr = new InputStreamReader(sk
.getInputStream());
BufferedReader br = new BufferedReader(isr);
String b = br.readLine(); // 读取
System.out.println("服务器响应说:" + b);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//服务器端
public class SocketServer {
public static void main(String[] args) {
new SocketServer().SocketServerTest();
}
public void SocketServerTest() {
try {
ServerSocket ss = new ServerSocket(1234); // 1234是端口
System.out.println("我是服务器1234,我启动了");
while (true) {
Socket s = ss.accept(); // 等待客户端来链接,返回一个socknt
while (true) {
// 读取s中传过来的数据
InputStreamReader isr = new InputStreamReader(s
.getInputStream());
BufferedReader br = new BufferedReader(isr);
String b = br.readLine(); // 读取
System.out.println("客户端说: " + b);
// // 在控制台输入
InputStreamReader isr2 = new InputStreamReader(System.in);
BufferedReader bdr = new BufferedReader(isr2);
String bd = bdr.readLine();// 等待输入
//.我们通过pw发数据。true即使刷新
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);// 链接成功的话。可以给服务器发送数据
pw.println(bd);// 服务器响应的数据
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
仅供参考。。。。
public class Client {
public static void main(String[] args) {
new Client().ClientTest();
}
public void ClientTest() {
try {
Socket sk = new Socket("127.0.0.1", 1234); // 链接服务器(链接ip为127.0.0.1,端口为1234)
while (true) {
InputStreamReader isr2 = new InputStreamReader(System.in);
BufferedReader bdr = new BufferedReader(isr2);
String bd = bdr.readLine();// 等待输入
PrintWriter pw = new PrintWriter(sk.getOutputStream(), true);// 链接成功的话。可以给服务器发送数据
// .我们通过pw发数据。true即使刷新
pw.println(bd);// 服务器响应的数据
// 接受服务器响应的数据
InputStreamReader isr = new InputStreamReader(sk
.getInputStream());
BufferedReader br = new BufferedReader(isr);
String b = br.readLine(); // 读取
System.out.println("服务器响应说:" + b);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//服务器端
public class SocketServer {
public static void main(String[] args) {
new SocketServer().SocketServerTest();
}
public void SocketServerTest() {
try {
ServerSocket ss = new ServerSocket(1234); // 1234是端口
System.out.println("我是服务器1234,我启动了");
while (true) {
Socket s = ss.accept(); // 等待客户端来链接,返回一个socknt
while (true) {
// 读取s中传过来的数据
InputStreamReader isr = new InputStreamReader(s
.getInputStream());
BufferedReader br = new BufferedReader(isr);
String b = br.readLine(); // 读取
System.out.println("客户端说: " + b);
// // 在控制台输入
InputStreamReader isr2 = new InputStreamReader(System.in);
BufferedReader bdr = new BufferedReader(isr2);
String bd = bdr.readLine();// 等待输入
//.我们通过pw发数据。true即使刷新
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);// 链接成功的话。可以给服务器发送数据
pw.println(bd);// 服务器响应的数据
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
仅供参考。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.*;
import java.net.*;
class TCPClient{
public static void main(String[] args)throws IOException{
Socket client = new Socket("127.0.0.1" , 5050);
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
out.write('c');
char c = (char)in.read();
System.out.println("收到:" + c);
out.close();
in.close();
client.close();
}
}
import java.io.*;
import java.net.*;
class TCPServer{
public static void main(String[] args)throws IOException{
ServerSocket listen = new ServerSocket(5050);
Socket server = listen.accept();
InputStream in = server.getInputStream();
OutputStream out = server.getOutputStream();
char c = (char)in.read();
System.out.println("收到:" + c);
out.write('s');
out.close();
in.close();
server.close();
listen.close();
}
}
import java.net.*;
class TCPClient{
public static void main(String[] args)throws IOException{
Socket client = new Socket("127.0.0.1" , 5050);
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
out.write('c');
char c = (char)in.read();
System.out.println("收到:" + c);
out.close();
in.close();
client.close();
}
}
import java.io.*;
import java.net.*;
class TCPServer{
public static void main(String[] args)throws IOException{
ServerSocket listen = new ServerSocket(5050);
Socket server = listen.accept();
InputStream in = server.getInputStream();
OutputStream out = server.getOutputStream();
char c = (char)in.read();
System.out.println("收到:" + c);
out.write('s');
out.close();
in.close();
server.close();
listen.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询