C#实现服务器和客户端通信问题

谢谢... 谢谢 展开
 我来答
益飇004
推荐于2016-03-27 · TA获得超过3307个赞
知道大有可为答主
回答量:4641
采纳率:25%
帮助的人:2950万
展开全部
import java.net.*;
import java.io.*;

public class Server {

public static void main(String[] args) throws IOException {
String s = "";//用stringbuffer试试
String s1 = "";
ServerSocket server = new ServerSocket(60000);
System.out.println("可以开始通信");

try {
Socket socket = server.accept();
System.out.println(socket);
InputStream is = socket.getInputStream();
OutputStream out = socket.getOutputStream();
BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

while (true) {
s = bfr.readLine();
System.out.println("客户端对我说:" + s);//读取客户端信息并输出
if("bye".equals(s)) {
pw.println("bye");
pw.flush();
break;
}

s1 = br.readLine();
pw.println(s1);
pw.flush();
System.out.println("我对客户端说:" + s1);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
server.close();
}

class Reader extends Thread {
public void run() {
while (true) {

}
}
}
}

import java.net.*;
import java.io.*;

public class Talk {

static InputStream is = null;
static OutputStream out;
static BufferedReader bfr;
static PrintWriter pw;
static BufferedReader pin;

public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("localhost"), 60000);
System.out.println(socket);

try {
is = socket.getInputStream();
out = socket.getOutputStream();
bfr = new BufferedReader(new InputStreamReader(is));
pw = new PrintWriter(new OutputStreamWriter(out));

pin = new BufferedReader(new InputStreamReader(System.in));

new Controlaa().start();

while (true) {

String s1 = bfr.readLine();
System.out.println("服务器对我说:" + s1);//读取服务器端发送的信息

if (s1.equals("bye")) {
break;
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

static class Controlaa extends Thread {

@Override
public void run() {
while (true) {
String s = "";
try {
s = pin.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("我对服务器说:" + s);
pw.println(s);
pw.flush();
}
}
}
}

简单搞了下,读取控制台输入和读取socket都是阻塞操作
迪凯特科技(北京)有限公司
2023-08-23 广告
要找到做串口联网服务器的公司,你可以采取以下措施:1. 在搜索引擎上输入关键词,例如“串口联网服务器公司”、“串口通信公司”等,并筛选搜索结果。2. 在行业门户网站或专业论坛上搜索相关帖子,向其他用户询问推荐可靠的串口联网服务器公司。3. ... 点击进入详情页
本回答由迪凯特科技(北京)有限公司提供
匿名用户
2011-07-06
展开全部
import java.net.*;
import java.io.*;

public class Server {

public static void main(String[] args) throws IOException {
String s = "";//用stringbuffer试试
String s1 = "";
ServerSocket server = new ServerSocket(60000);
System.out.println("可以开始通信");

try {
Socket socket = server.accept();
System.out.println(socket);
InputStream is = socket.getInputStream();
OutputStream out = socket.getOutputStream();
BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

while (true) {
s = bfr.readLine();
System.out.println("客户端对我说:" + s);//读取客户端信息并输出
if("bye".equals(s)) {
pw.println("bye");
pw.flush();
break;
}

s1 = br.readLine();
pw.println(s1);
pw.flush();
System.out.println("我对客户端说:" + s1);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
server.close();
}

class Reader extends Thread {
public void run() {
while (true) {

}
}
}
}

import java.net.*;
import java.io.*;

public class Talk {

static InputStream is = null;
static OutputStream out;
static BufferedReader bfr;
static PrintWriter pw;
static BufferedReader pin;

public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("localhost"), 60000);
System.out.println(socket);

try {
is = socket.getInputStream();
out = socket.getOutputStream();
bfr = new BufferedReader(new InputStreamReader(is));
pw = new PrintWriter(new OutputStreamWriter(out));

pin = new BufferedReader(new InputStreamReader(System.in));

new Controlaa().start();

while (true) {

String s1 = bfr.readLine();
System.out.println("服务器对我说:" + s1);//读取服务器端发送的信息

if (s1.equals("bye")) {
break;
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

static class Controlaa extends Thread {

@Override
public void run() {
while (true) {
String s = "";
try {
s = pin.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("我对服务器说:" + s);
pw.println(s);
pw.flush();
}
}
}
}

简单搞了下,读取控制台输入和读取socket都是阻塞操作
另外,团IDC网上有许多产品团购,便宜有口碑
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式