Python与Java间Socket通信
之前做过一款Java的通讯工具,有发消息发文件等基本功能.可大家也都知道Java写的界面无论是AWT或Swing,那简直不是人看的,对于我们这些开发人员还好,如果是Release出去给用户看,那必须被鄙视到底.用C++的话,写的代码也是非常多的(QT这方面做得很好!),但我这里改用Python,以便到时用wxPython做界面.而且这两者跨平台也做得非常好.
这里只给出核心实现以及思路
Server(Java)接收从Clinet(Python)发送来的文件
JServer.java
import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class JServer implements Runnable { ServerSocket ss; public JServer() throws Exception { ss = new ServerSocket(8086); new Thread(this).start(); } @Override public void run() { int i = 0; System.out.println("server startup."); while (true) { try { Socket s = ss.accept(); // 每个客户端一个处理线程 new Handler(s, i).start(); i++; } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { try { new JServer(); } catch (Exception e) { e.printStackTrace(); } } } class Handler extends Thread { Socket s; int id; public Handler(Socket s, int id) { this.s = s; this.id = id; } @Override public void run() { System.out.println("in handling.."); FileOutputStream fos = null; try { InputStream is = s.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); // 从客户端读取发送过来的文件名 String filename = in.readLine(); System.out.println("read line " + id + " :" + filename); File file = new File(filename); int len = 0; int BUFSIZE = 4*1024; byte[] by = new byte[BUFSIZE * 1024]; fos = new FileOutputStream(file); while ((len = is.read(by, 0, BUFSIZE)) != -1) { fos.write(by, 0, len); fos.flush(); } System.out.println("done."); } catch (Exception e) { e.printStackTrace(); } finally { // 服务端就不要手贱 关了socket否则Python 会出现错误Errno 10054让客户端关掉就行啦 try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Python客户端
# -*- coding: utf-8 -*- #!/usr/bin/python #coding=utf-8 import time import threading import socket import os class Client(): def __init__(self): address = ('127.0.0.1', 8086) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(address) fn = 'test.zip' ff = os.path.normcase(fn) try: f = open(fn, 'rb') sendFile = SendFile(s,f) sendFile.start() print 'start to send file.' except IOError: print 'open err' class SendFile(threading.Thread): def __init__(self, sock, file): threading.Thread.__init__(self) self.file = file self.sock = sock def run(self): print self.file BUFSIZE = 1024 count = 0 name = self.file.name+'\r' # 前1k字节是为了给服务端发送文件名 一定要加上'\r',不然服务端就不能readline了 for i in range(1, BUFSIZE - len(self.filename) -1): name += '?' print name self.sock.send(name) while True: print BUFSIZE fdata = self.file.read(BUFSIZE) if not fdata: print 'no data.' break self.sock.send(fdata) count += 1 if len(fdata) != BUFSIZE: print 'count:'+str(count) print len(fdata) nRead = len(fdata) print 'send file finished.' self.file.close() self.sock.close() print 'close socket' c = Client()
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。