用java socket编写的server端,用python编写的client,数据怎么传输,最好给个具体的例子,谢谢!
如,我用java写的server端程序:publicDatouServer(){try{server=newServerSocket(PORT);socket=serve...
如,我用java写的server端程序:
public DatouServer() {
try {
server = new ServerSocket(PORT);
socket = server.accept();
String line;
is = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
os = new PrintWriter(socket.getOutputStream());
sin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client:" + is.readLine());
System.out.println("Server Input:");
line = sin.readLine();
while (!line.equals("bye")) {
os.println(line);
os.flush();
System.out.println("Server:" + line);
System.out.println("Client:" + is.readLine());
line = sin.readLine();
}
os.close();
is.close();
socket.close();
server.close();
} catch (Exception e) {
System.out.println("Error:" + e);
}
}
public static String nowTime() {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(new Date().getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss:SS");
return dateFormat.format(c.getTime());
}
public void setData(String data) {
this.data = data;
}
public String getData() {
return this.data;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new DatouServer();
}
}
我用python写的客户端:
import socket
import time
if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 10000))
while True:
try:
time.sleep(2)
print 'time'
#clientdata = input()
# sock.send(clientdata)
sock.sendall('1')
print 'send'
print sock.recv(1024)
print 'recv'
except socket.error:
print 'error'
sock.close()
我客户端发送的消息服务器怎么接受,服务器怎么给客户端发送消息? 展开
public DatouServer() {
try {
server = new ServerSocket(PORT);
socket = server.accept();
String line;
is = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
os = new PrintWriter(socket.getOutputStream());
sin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client:" + is.readLine());
System.out.println("Server Input:");
line = sin.readLine();
while (!line.equals("bye")) {
os.println(line);
os.flush();
System.out.println("Server:" + line);
System.out.println("Client:" + is.readLine());
line = sin.readLine();
}
os.close();
is.close();
socket.close();
server.close();
} catch (Exception e) {
System.out.println("Error:" + e);
}
}
public static String nowTime() {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(new Date().getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss:SS");
return dateFormat.format(c.getTime());
}
public void setData(String data) {
this.data = data;
}
public String getData() {
return this.data;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new DatouServer();
}
}
我用python写的客户端:
import socket
import time
if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 10000))
while True:
try:
time.sleep(2)
print 'time'
#clientdata = input()
# sock.send(clientdata)
sock.sendall('1')
print 'send'
print sock.recv(1024)
print 'recv'
except socket.error:
print 'error'
sock.close()
我客户端发送的消息服务器怎么接受,服务器怎么给客户端发送消息? 展开
4个回答
展开全部
python不熟悉,不过我写过C和java通信的例子,socket通信,统一用byte处理,你只要考虑发送和接收时编码格式一致,别乱码就好了
追问
我跟踪发现,sock.send可以吧数据发送出去,但是python好像没有一个像os.flush();这样的方法,使得send的数据立马让server端可以收到,如果这样的话,client端后面跟一个recv方法,由于server端没有接收到send的数据,也就没有返回的数据,导致client端会卡在recv方法处……
追答
python难道不能写数据流吗,你这你该解决的问题了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看看书吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用字节传输呗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询