圖形介面的TCP/IP簡單通訊

QSXXN發表於2020-10-09

圖形介面的TCP/IP簡單通訊

前言

只有簡單的收發訊息功能,因為太簡單被駁回,寫個部落格留做紀念

語言

python3

程式設計環境

pycharm

執行截圖

在這裡插入圖片描述
在這裡插入圖片描述

原始碼

伺服器端:

import socket


def main():
    
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

   
    tcp_server_socket.bind(("", 7890))


    tcp_server_socket.listen(128)

    print("-----1----")

    new_client_socket, client_addr = tcp_server_socket.accept()
    print("-----2----")

    print(client_addr)

    # 接收客戶端傳送過來的請求
    recv_data = new_client_socket.recv(1024)
    print(recv_data)

    # 回送一部分資料給客戶端
    new_client_socket.send("hahahghai-----ok-----".encode("utf-8"))

    # 關閉套接字
    new_client_socket.close()
    tcp_server_socket.close()

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tkinter
import datetime
import tkinter.messagebox
import socket
def func():
    print(text2.get("0.0", "end"))
    print(text3.get("0.0", "end"))
    t1=text2.get("0.0", "end")
    t2=text3.get("0.0", "end")
    t1=t1.replace('\n','')
    t2=t2.replace('\n','')
    if t2=="":
        tkinter.messagebox.askokcancel('訊息', '埠不能為空!請重新輸入')
        return;
    port=int(t2)
    # 1. 買個手機(建立套接字 socket)
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 2. 插入手機卡(繫結本地資訊 bind)
    tcp_server_socket.bind((t1, port))
    text5.insert(tkinter.INSERT, "IP地址:" + t1 + "  埠號:" + t2 + "\n")
    tcp_server_socket.listen(128)
    print("-----1----")
    # 4. 等待別人的電話到來(等待客戶端的連結 accept)
    new_client_socket, client_addr = tcp_server_socket.accept()
    print("-----2----")
    ip, port1 = client_addr
    print(client_addr)

    # 接收客戶端傳送過來的請求
    recv_data = new_client_socket.recv(1024)
    print(recv_data)
    text5.insert(tkinter.INSERT, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "\n")
    text5.insert(tkinter.INSERT, "Receive from " + ip + " %d" % port1 + ":" + recv_data.decode('utf-8') + "\n")
    # 回送一部分資料給客戶端
    win.update()
    new_client_socket.send("Hello!".encode("utf-8"))

    # 關閉套接字
    new_client_socket.close()
    tcp_server_socket.close()



# 建立主視窗
win = tkinter.Tk()
# 設定標題
win.title("tcp伺服器端")
# 設定大小和位置
win.geometry("500x400")

# 進入訊息迴圈,可以寫控制元件
ti=tkinter.Label(win, width=10, height=1,font=("仿宋",20),text="TCP伺服器",justify="left")
text = tkinter.Label(win, width=10, height=1,font=("仿宋",15),text="IP地址",justify="center",bg="white")
text1 = tkinter.Label(win, width=10, height=1,font=("仿宋",15),text="埠號",justify="center",bg="white")
text2=tkinter.Text(win, width=30, height=1,font=("仿宋",15))
text3=tkinter.Text(win, width=30, height=1,font=("仿宋",15))
text2.insert(tkinter.INSERT, "127.0.0.1")
text3.insert(tkinter.INSERT, "")
button1 = tkinter.Button(win, text="開啟", command=func, width=8, height=1,font=("仿宋",12))
text4 = tkinter.Label(win, width=10, height=1,font=("仿宋",12),text="伺服器日誌",justify="center")
text5=tkinter.Text(win, width=45, height=20,font=("仿宋",10))
win.update()
ti.grid(row=0,column=1)
text.grid(row=1,column=0)
text1.grid(row=2,column=0)
text2.grid(row=1,column=1)
text3.grid(row=2,column=1)
text4.grid(row=3,column=0)
text5.grid(row=4,column=1)
button1.grid(row=2,column=2)
win.mainloop()

客戶端:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tkinter
import datetime
import tkinter.messagebox
import socket
def func():
    print(text2.get("0.0", "end"))
    print(text3.get("0.0", "end"))
    print(text6.get("0.0", "end"))
    t1=text2.get("0.0", "end")
    t2=text3.get("0.0", "end")
    t3=text6.get("0.0", "end")
    t1=t1.replace('\n','')
    t2=t2.replace('\n','')
    t3=t3.replace('\n', '')
    if t2=="":
        tkinter.messagebox.askokcancel('訊息', '埠不能為空!請重新輸入')
        return;
    port=int(t2)
    # 1. 建立tcp的套接字
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 2. 連結伺服器
    # tcp_socket.connect(("192.168.33.11", 7890))
    server_addr = (t1, port)
    tcp_socket.connect(server_addr)

    # 3. 傳送資料/接收資料
    tcp_socket.send(t3.encode("utf-8"))
    text5.insert(tkinter.INSERT, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "\n")
    response = tcp_socket.recv(1024)
    text5.insert(tkinter.INSERT, "response from " + t1 + ":" + response.decode('utf-8') + "\n")
    # 4. 關閉套接字
    tcp_socket.close()


# 建立主視窗
win = tkinter.Tk()
# 設定標題
win.title("tcp客戶端")
# 設定大小和位置
win.geometry("500x400")

# 進入訊息迴圈,可以寫控制元件
ti=tkinter.Label(win, width=10, height=1,font=("仿宋",20),text="TCP客戶端",justify="left")
text = tkinter.Label(win, width=10, height=1,font=("仿宋",15),text="IP地址",justify="center",bg="white")
text1 = tkinter.Label(win, width=10, height=1,font=("仿宋",15),text="埠號",justify="center",bg="white")
text2=tkinter.Text(win, width=30, height=1,font=("仿宋",15))
text3=tkinter.Text(win, width=30, height=1,font=("仿宋",15))
text2.insert(tkinter.INSERT, "127.0.0.1")
text3.insert(tkinter.INSERT, "")
button1 = tkinter.Button(win, text="傳送", command=func, width=8, height=1,font=("仿宋",12))
text4 = tkinter.Label(win, width=10, height=1,font=("仿宋",12),text="客戶端日誌",justify="center")
text5=tkinter.Text(win, width=45, height=18,font=("仿宋",10))
text6=tkinter.Text(win, width=30, height=1,font=("仿宋",15))
text7 = tkinter.Label(win, width=10, height=1,font=("仿宋",15),text="訊息",justify="center",bg="white")
ti.grid(row=0,column=1)
text.grid(row=1,column=0)
text1.grid(row=2,column=0)
text2.grid(row=1,column=1)
text3.grid(row=2,column=1)
text4.grid(row=4,column=0)
text7.grid(row=3,column=0)
text6.grid(row=3,column=1)
text5.grid(row=5,column=1)
button1.grid(row=3,column=2)
win.mainloop()

總結:現在就是後悔,非常後悔

相關文章