[python语法语义] python tcp连接问题

xlaohe1 2012-09-26
我用写了个tcp连接程序
启动服务端,启动客户端
然后客户端与服务端可以互相发送消息
我这程序有个问题,当1方发送了消息,另一方并不能立马显示
只有另1放也发送了消息才能显示
还有,只能1次发送1次
请指定,菜鸟,刚学
server
# -*- coding:utf-8 -*-

import socket

def tcp_server():
    host = 'localhost'
    port = 35105
    buf_size = 1024
    addr = (host, port)
    
    tcp_sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_sc.bind(addr)
    tcp_sc.listen(5)
    
    print 'Waiting for connect...'
    
    while True:
        global  flag
        tcp_cc, addr = tcp_sc.accept()
        tcp_cc.send('hello')
        while True:
            s_msg = raw_input('Enter send message:\n')
            tcp_cc.send(s_msg)
            if s_msg == 'quit':
                flag = False
                break
            msg = tcp_cc.recv(buf_size)
            if len(msg) == 0:
                continue
            print msg
            if msg == 'quit':
                flag = False
                break
        tcp_cc.close()
        if not flag:
            break
        
    tcp_sc.close()
    
    
if __name__ == '__main__':
    tcp_server()


client:

# -*- coding:utf-8 -*-

import socket

def tcp_client():
    host = 'localhost'
    port = 35105
    buf_size = 1024
    addr = (host, port)
    
    tcp_cc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_cc.connect(addr)
    
    print 'Connect to server...'
    
    tcp_cc.send('hello server')
    while True:
        s_msg = raw_input('Enter send message:\n')
        tcp_cc.send(s_msg)
        if s_msg == 'quit':
            break
        msg = tcp_cc.recv(buf_size)
        if len(msg) == 0:
            continue
        print msg
        if msg == 'quit':
            break
    tcp_cc.close()
    
if __name__ == '__main__':
    tcp_client()
Global site tag (gtag.js) - Google Analytics