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

2024-12-28 10:55:47
推荐回答(3个)
回答1:

我想可以建两个WebService接口用于通信。再建一个FTP服务器用于下载。客户端通过WebService传递信息到服务器端。服务器端根据信息生成文件。上传到FTP服务器上(如果FTP服务器和通信服务器是一个保存到本地就可以了)。同时通知客户端下载。并把下载路径传给客户端。客户端下载。

回答2:

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都是阻塞操作

回答3:

自动解压不会。另外你向壳户端传的是大文件的话,这就要考虑多线程技术了,用socket通信来做,你先查查多线程socket通信技术吧,还有消息队列