[問題] winsock 的 傳送問題

看板C_and_CPP (C/C++)作者 (Deathful)時間14年前 (2011/07/20 22:00), 編輯推噓1(109)
留言10則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Dev C++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): send()函式中 server端 傳送"字串"給 client端 想請問 如果一定得傳"整數"型態 那send()要怎用? 還是根本不能用? 如果不能用 那有其他函式用嗎? 餵入的資料(Input):預期的正確結果(Expected Output):錯誤結果(Wrong Output):程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): 程式目的大約是: server端要將"字串"用hton轉換存成int型態 再以int型態傳送到client端 client端要將int型態用ntoh轉換成字串 這麼大費周章是有其他原因... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.41.121.59

07/20 22:03, , 1F
int i=10; send(s, (char *)&i, sizeof(int), 0);
07/20 22:03, 1F

07/20 22:04, , 2F
不過hton跟ntoh用途並不是字串與數字的轉換...
07/20 22:04, 2F

07/20 22:08, , 3F
再請問 那收端recv()要怎寫?
07/20 22:08, 3F

07/20 22:10, , 4F
int i; recv(s, (char *)&i, sizeof(int), 0);
07/20 22:10, 4F

07/20 22:11, , 5F
因為要跨平台 server是WinXP client是WinCE
07/20 22:11, 5F

07/20 22:16, , 6F
完整一點的寫法是 http://pastie.org/2243007
07/20 22:16, 6F

07/20 22:28, , 7F
那我要跨平台 是不是就得用到 hton ntoh 囉?
07/20 22:28, 7F

07/20 22:28, , 8F
也就是說 hton ntoh 是在跨平台時用到?
07/20 22:28, 8F

07/20 23:07, , 9F
感謝james大 小弟已經可以順利傳送字母過去了
07/20 23:07, 9F

07/21 03:43, , 10F
http://goo.gl/Ad9SF wiki/Endianness in networking
07/21 03:43, 10F
文章代碼(AID): #1E9j-Lfb (C_and_CPP)
文章代碼(AID): #1E9j-Lfb (C_and_CPP)