[問題] 利用C寫網路傳送程式問題

看板C_and_CPP作者 (好好壞壞~~)時間8年前 (2016/03/03 23:29), 8年前編輯推噓2(207)
留言9則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 問題(Question): 想在PC上利用C語言寫一個網路傳送程式傳送封包到一台機器上(IP:192.168.1.55), 查了一般的socket寫法, 程式碼摘要如下: if((m_sockXmt = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET){ return; } m_st_xmt.sin_family = AF_INET; m_st_xmt.sin_port = htons(40000); m_st_xmt.sin_addr.s_addr = inet_addr("192.168.1.55"); 經執行上述程式碼後,在wireshark軟體上所看如下: 192.168.1.1 192.168.1.55 UDP 50 Source port:57956 Destination port:40000 由以上可以看到Destination port已經被設定為40000,機器的ip:192.168.1.55 也設定正確了。 想請問如果我也想設定Source port為固定值, 譬如我每次開始執行程式時,皆可從wireshark軟體上看到 Source port被設定為50000。 想請問該如何再加程式碼才可以讓我的Source port被設定為固定值50000,謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.72.175.103 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1457018981.A.736.html ※ 編輯: smvenjoy (42.72.175.103), 03/03/2016 23:31:37

03/03 23:54, , 1F
send之前先bind
03/03 23:54, 1F

03/04 00:09, , 2F
樓上可以再說明詳細一點嗎? 謝謝!
03/04 00:09, 2F

03/04 00:19, , 3F
bind(m_sockXmt,(LPSOCKADDR)&m_st_xmt,sizeof(m_st_xmt
03/04 00:19, 3F

03/04 00:21, , 4F
) 有在send前加此行指令,但SourcePortNumber仍不為定值
03/04 00:21, 4F

03/04 21:39, , 5F
bind(dest_sock, &src_addr, sizeof(src_addr))
03/04 21:39, 5F

03/04 21:43, , 6F
你bind的address struct看起來是目標
03/04 21:43, 6F

03/05 20:20, , 7F
srcport系統會自已給,如果你硬要,那你就自已刻封包,
03/05 20:20, 7F

03/05 20:20, , 8F
用rawsocket的方式去傳.
03/05 20:20, 8F

03/05 20:20, , 9F
文章代碼(AID): #1Ms5XbSs (C_and_CPP)