電腦遊戲製作開發設計論壇 首頁 電腦遊戲製作開發設計論壇
任何可以在PC上跑的遊戲都可以討論,主要以遊戲之製作開發為主軸,希望讓台灣的遊戲人有個討論、交流、教學、經驗傳承的園地
 
 常見問題常見問題   搜尋搜尋   會員列表會員列表   會員群組會員群組   會員註冊會員註冊 
 個人資料個人資料   登入檢查您的私人訊息登入檢查您的私人訊息   登入登入 

Google
socket的步驟心得

 
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 網路程式設計
上一篇主題 :: 下一篇主題  
發表人 內容
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2007-10-11, AM 4:25 星期四    文章主題: socket的步驟心得 引言回覆

其實我是才剛學,對於提供半生不熟的技術,會造成什麼後果不負責
目的是希望拋磚引玉,引發大家的討論...
它原本是一個聊天範例,所以我不寫一樣的出來的。
改天再寫個範例,大家應該會比較了解,目前還沒想到要寫什麼
如果有什麼遺漏的地方,到時再補充吧!!

首先來大致介紹一些socket的步驟
書上有的地方並沒有寫的詳細
1.要先引入LIB檔,在這使用#pragma comment(lib,"WSock32.lib");
2.初始化,要先調用WSAStartup()函數,才可以建立socket

【多人連線遊戲程式設計開發手冊 之補充 (Winsock 2)】
來源:http://baby.imagenet.com.tw/modules.php?name=News&file=article&sid=37
Windows 95 =>1.1(2.2)
Windows 98 =>2.2
Windows Me =>2.2
Windows NT4.0 =>2.2
Windows 2000 =>2.2
Windows XP =>2.2
Windows CE =>1.1

以XP來說,只要調用下方的範例即可
WSAData wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);


3.接下來宣告一個Server用,一個Client用的socket,並進行初始化

(建立socket的說明可在MSDN中查到它的使用方法)
/******************************************************************************/
The Windows Sockets socket function creates a socket that is bound to a specific service provider.

SOCKET socket (
int af, //設定AF_INET即可
int type, //設定SOCK_STREAM即可
int protocol //設定0即可
);

Parameters
af
[in] An address family specification.
type
[in] A type specification for the new socket.
The following are the only two type specifications supported for Windows Sockets 1.1: Type Explanation
SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams with an out-of-band data transmission mechanism. Uses TCP for the Internet address family.
SOCK_DGRAM Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family.



In Windows Sockets 2, many new socket types will be introduced and don' need to be specified now because an application can dynamically discover the attributes of each available transport protocol through the WSAEnumProtocols function. Socket type definitions will appear in WINSOCK2.H, which will be periodically updated as new socket types, address families and protocols are defined.

protocol
[in] A particular protocol to be used with the socket that is specific to the indicated address family.
/******************************************************************************/
回頂端
檢視會員個人資料 發送私人訊息
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2007-10-11, AM 4:27 星期四    文章主題: 引言回覆

4.建立訊息回應CLIENT_MESSAGE跟SERVER_MESSAGE
5.用WSAAsyncSelect設定非同步訊息

WSAAsyncSelect的說明如下(因為太長了,所以不貼上其它說明)
/******************************************************************************/
The Windows Sockets WSAAsyncSelect function requests Windows message-based notification of network events for a socket.

int WSAAsyncSelect (
SOCKET s,
HWND hWnd,
unsigned int wMsg, //使用的訊息回應client用CLIENT_MESSAGE,server用SERVER_MESSAGE
long lEvent //事件(下列參考)
);


The lEvent parameter is constructed by using the bitwise OR operator with any of the values specified in the following list.

Value Meaning
FD_READ Want to receive notification of readiness for reading
FD_WRITE Want to receive notification of readiness for writing
FD_OOB Want to receive notification of the arrival of out-of-band data
FD_ACCEPT Want to receive notification of incoming connections
FD_CONNECT Want to receive notification of completed connection or multi-point join operation
FD_CLOSE Want to receive notification of socket closure
FD_QOS Want to receive notification of socket Quality of Service (QOS) changes
FD_GROUP_QOS Want to receive notification of socket group Quality of Service (QOS) changes (reserved for future use with socket groups)
FD_ROUTING
_INTERFACE_CHANGE Want to receive notification of routing interface changes for the specified destination(s)
FD_ADDRESS_LIST
_CHANGE Want to receive notification of local address list changes for the socket's protocol family

/******************************************************************************/

6.調用sockaddr_in設定監聽資訊

/******************************************************************************/
SOCKADDR_IN Structure
The SOCKADDR_IN structure has the following form:

struct sockaddr_in{
short sin_family; //設定AF_INET
unsigned short sin_port; //htons(調用端口)
struct in_addr sin_addr; //設定INADDR_ANY
char sin_zero[8];
};

In the Internet address family, the SOCKADDR_IN structure is used by Windows Sockets to specify a local or remote endpoint address to which to connect a socket. This is the form of the SOCKADDR structure specific to the Internet address family and can be cast to SOCKADDR.

Members

sin_family
Address family (must be AF_INET).

sin_port
IP port.

sin_addr
IP address.

sin_zero
Padding to make structure the same size as SOCKADDR.
/******************************************************************************/


mirror 在 2007-10-11, AM 8:41 星期四 作了第 1 次修改
回頂端
檢視會員個人資料 發送私人訊息
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2007-10-11, AM 4:27 星期四    文章主題: 引言回覆

7.用bind綁定監聽

/******************************************************************************/
bind
The Windows Sockets bind function associates a local address with a socket.

int bind (
SOCKET s,
const struct sockaddr FAR* name, //指sockaddr_in
int namelen //sockaddr_in的資料長度
);

Parameters
s
[in] A descriptor identifying an unbound socket.

name
[in] The address to assign to the socket from the SOCKADDR structure.

namelen
[in] The length of the name.
/******************************************************************************/

8.listen把socket設置成等待client的連線狀態
9.client端用connect向伺服器連結(用法與bind相似)
10.處理server的FD_ACCEPT訊息,用accept接收client並生成可用的socket

/******************************************************************************/
相關資料如下
結構:

//全域變數
std::vector<SOCKET> g_DataSockets; //紀錄client端的socket資料

//區域變數
SOCKET socket;
sockaddr_in name;
int namelen; //name的資料長度

//FD_ACCEPT訊息配置
socket = accept(g_ListenSocket,NULL,NULL);
getpeername(socket,(sockaddr*)&name,&namelen);
g_DataSockets.push_back(socket); //增加新的socket資料

/******************************************************************************/

11.處理FD_訊息(包括讀取、寫入)
12.利用send發送訊息(server與clietn之間的訊息傳送)
client調用自己的socket即可
server調用g_DataSockets(儲存的socket資料)即可發送訊息給各client端

/******************************************************************************/
The Windows Sockets send function sends data on a connected socket.

int send (
SOCKET s,
const char FAR * buf,
int len,
int flags
);

Parameters
s
[in] A descriptor identifying a connected socket.

buf
[in] A buffer containing the data to be transmitted.

len
[in] The length of the data in buf.

flags
[in] An indicator specifying the way in which the call is made.
/******************************************************************************/

12.用closesocket關閉socket
回頂端
檢視會員個人資料 發送私人訊息
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2007-10-12, AM 1:17 星期五    文章主題: 引言回覆

大家按下面連結下載
http://www.badongo.com/file/4678240
密碼:1012
只有寫到客戶端連向伺服器端
並沒有做深入的處理
有興趣的人先學習前面的部分吧
vs2005寫的
用的方法,先開啟server
然後把client底下的ini檔改為server的ip可以了
server接受連結訊息會跳出通知...
大致上就這樣,只有寫前面很簡單的部分

基本我是有一點懶,想說做一個聊天室
第一間市集,第二間道具屋之類,第三間野外
大家可以依喜好進到喜歡的聊天室
然後會不時出現怪物攻擊玩家,當然是只有文字表現
大家可以聊天看戲,也可以下指令攻擊怪物
不過我也可能花些時間完成美工
做一個無聊的小遊戲出來
回頂端
檢視會員個人資料 發送私人訊息
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2007-10-13, AM 10:01 星期六    文章主題: 引言回覆

我們來給程式加一點東西,當作是練習就好
各位可以發揮想像力,給這個程式加點什麼來好
另外BUFFER_SIZE的數值可定義小一點

代碼:
//處理客戶端訊息
int OnClientMessage(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
   char buffer[BUFFER_SIZE+1]="";
   int retCode;
   static int k;

   char c[1000];

   switch(WSAGETSELECTEVENT(lParam))
   {
   case FD_CONNECT:
         //MessageBox(hWnd,"連接向伺服器","",IDOK);
         break;
   case FD_READ:
      retCode = recv(g_ClientSocket,buffer,BUFFER_SIZE,0);
      if(retCode!=SOCKET_ERROR)
      {
         switch(buffer[0])
         {
         case '1':
            MessageBox(hWnd,&buffer[1],"",IDOK);
            break;
         case '2':
            MessageBox(hWnd,&buffer[1],"",IDOK);
            break;
         case '3':
            SetWindowText(hWnd,&buffer[1]);
            break;
         }
      }

      break;
   }

   return 0;
}


接下來是伺服器端的

代碼:
//處理伺服器訊息
int OnServerMessage(HWND hWnd,WPARAM wParam,LPARAM lParam)
{
   SOCKET socket;
   char buffer[BUFFER_SIZE+1]="";
   int i,rectCode,namelen;

   namelen = sizeof(buffer);

   switch(WSAGETSELECTEVENT(lParam))
   {
   case FD_ACCEPT:
      //連接訊息
      socket = accept(g_ListenSocket,NULL,NULL);
      
      sprintf(&buffer[0],"3初心者視窗!");
      send(socket,&buffer[0],namelen,1);

      sprintf(&buffer[0],"2☆☆!");
      send(socket,&buffer[0],namelen,1);

      sprintf(&buffer[0],"1歡迎呀!");
      send(socket,&buffer[0],namelen,1);
      break;
   }

   return 0;
}
回頂端
檢視會員個人資料 發送私人訊息
Sompongjin
稍嫌羞澀的路人


註冊時間: 2020-06-12
文章: 2

10.63 果凍幣

發表發表於: 2020-6-15, PM 5:21 星期一    文章主題: 引言回覆

我认为这是一个非常好的网站。
回頂端
檢視會員個人資料 發送私人訊息
從之前的文章開始顯示:   
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 網路程式設計 所有的時間均為 台灣時間 (GMT + 8 小時)
1頁(共1頁)

 
前往:  
無法 在這個版面發表文章
無法 在這個版面回覆文章
無法 在這個版面編輯文章
無法 在這個版面刪除文章
無法 在這個版面進行投票
可以 在這個版面附加檔案
可以 在這個版面下載檔案


Powered by phpBB © 2001, 2005 phpBB Group
正體中文語系由 phpbb-tw 維護製作