遊戲網路通訊方式基於TCP

2021-06-26 13:45:16 字數 3390 閱讀 6000

server:

#include "stdafx.h"

#include< stdio.h >

#include< stdlib.h >

#include< windows.h >

#include< winsock.h >

#include< string.h >

#pragma comment( lib, "ws2_32.lib" )

#define port 2046

#define backlog 10

#define true 1

int _tmain(int argc, _tchar* argv)

*/ //把埠號轉化成整數

/*short port;

if ((port = atoi(argv[1])) == 0)*/

wsadata wsa;

//初始化套接字dll

if (wsastartup(makeword(2, 2), &wsa) != 0)

//建立套接字

socket serversocket;

if ((serversocket = socket(af_inet, sock_stream, ipproto_tcp)) == invalid_socket)

struct sockaddr_in serveraddress;

memset(&serveraddress, 0, sizeof(sockaddr_in));

serveraddress.sin_family = af_inet;

serveraddress.sin_addr.s_un.s_addr = htonl(inaddr_any);

serveraddress.sin_port = htons(port);

//繫結

if (bind(serversocket, (sockaddr*)&serveraddress, sizeof(serveraddress)) == socket_error)

//進入偵聽狀態

if (listen(serversocket, somaxconn) == socket_error)

printf("server %d is listening......\n", port);

while (true)

printf("accept connection from %s\n", inet_ntoa(clientaddress.sin_addr));

char buf[4096];

//while (1)

buf[bytes] = '\0';

printf("message from %s: %s\n", inet_ntoa(clientaddress.sin_addr), buf);

if (send(clientsocket, buf, bytes, 0) == socket_error)

closesocket(clientsocket);

//清理套接字占用的資源

printf("%s exit!\n", inet_ntoa(clientaddress.sin_addr));

} wsacleanup();

return 0;

}

client:

#include "stdafx.h"

#include< stdio.h >

#include< stdlib.h >

#include< windows.h >

#include< winsock.h >

#include< string.h >

#pragma comment( lib, "ws2_32.lib" )

#define port 2046

#define backlog 10

#define true 1

#define maxdatasize 100

int _tmain(int argc, _tchar* argv)

*/ //把字串的ip位址轉化為u_long

unsigned long ip = inet_addr("127.0.0.1");

/*if ((ip = inet_addr(argv[1])) == inaddr_none)*/

//把埠號轉化成整數

//short port;

/*if ((port = atoi(argv[2])) == 0)*/

printf("connecting to %s:%d......\n", inet_ntoa(*(in_addr*)&ip), port);

wsadata wsa;

//初始化套接字dll

if (wsastartup(makeword(2, 2), &wsa) != 0)

while (1)

struct sockaddr_in serveraddress;

memset(&serveraddress, 0, sizeof(sockaddr_in));

serveraddress.sin_family = af_inet;

serveraddress.sin_addr.s_un.s_addr = ip;

serveraddress.sin_port = htons(port);

//建立和伺服器的連線

if (connect(sock, (sockaddr*)&serveraddress, sizeof(serveraddress)) == socket_error)

char buf[4096];

printf(">");

//從控制台讀取一行資料

gets(buf);

int index = 0;

while (buf[index])

//傳送給伺服器

if (send(sock, buf, strlen(buf), 0) == socket_error)

int bytes;

if ((bytes = recv(sock, buf, sizeof(buf), 0)) == socket_error)

index = 0;

while (buf[index])

buf[bytes] = '\0';

printf("message from %s: %s\n", inet_ntoa(serveraddress.sin_addr), buf);

closesocket(sock);

} //清理套接字占用的資源

wsacleanup();

return 0;

}

網路通訊,基於TCP協議,C ,實現

tcp協議是乙個基本的網路協議,基本上所有的網路服務都是基於tcp協議的,如http,ftp 等等,所以要了解網路程式設計就必須了解基於tcp協議的程式設計。然而tcp協議是乙個龐雜的體系,要 徹底的弄清楚它的實現不是一天兩天的功夫,所幸的是在.net framework環境下,我們不必要去 部分 ...

Python 基於 TCP 傳輸協議的網路通訊實現

什麼是網路程式設計?指在網路環境中,如何實現不在同一物理位置中的計算機之間進行資料通訊 如果要保證資料通訊順利完成,則需要先了解如下幾個概念 不同計算機內的程序之間進行資料通訊時,需要先對資料進行封裝或打包後方可以進行傳輸。所謂協議指通訊雙方需要共同遵守的資料打包格式。如同現實世界裡郵寄商品一樣 商...

linux網路通訊二(TCP)

tcp通訊 流程圖如下 1 建立 socket套接字 相同2 繫結 將建立的socket套接字返回的控制代碼與本機的ip 埠繫結 相同3 監聽 監聽socket include int listen int s,int backlog s 監聽的控制代碼 backlog 監聽的最大數,預設20 成功...