python与java间socket通信实例代码分析

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()

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多python与java间socket通信实例代码分析相关文章请关注php中文网!

Posted in 未分类

发表评论