TCP Socket通訊學習

2021-06-06 08:52:27 字數 4532 閱讀 1139

學習內容,參見《windows網路程式設計》 第7章 winsock基礎

伺服器/客戶端連線模型見下圖。

服務端流程

客戶端流程

使用windows的winsock 2程式設計,建立win32控制台工程,進行工程配置。

服務端**,tcpserver.cpp。

//#include "stdafx.h"

#include #include #include #define default_port 5150

#define default_buffer 4096

int iport = default_port;//port to listen for clients on

bool binte***ce = false;//listen on the specified inte***ce

bool brecvonly = false;//receive data only; don't echo back

tchar szaddress[128];//inte***ce to listen for clients on

//print usage information and exit

void usage()

//parse the command line arguments, and set some global flags

//to indicate what actions to perform

void validateargs(int agrc, _tchar** argv)

break;

case _t('o'):

//只接受資料

brecvonly = true;

break;

default:

usage();

break;}}

}}dword winapi clientthread(lpvoid lpparam)

else if (socket_error == ret)

szbuff[ret] = '\0';

printf("recv:'%s'\n", szbuff);

//if we selected to echo the data back, do it

if (!brecvonly)

else if (socket_error == ret)

nleft -= ret;

idx += ret;}}

}return 0;

}int _tmain(int argc, _tchar* argv)

//create our listening socket

socket slisten = socket(af_inet, sock_stream, ipproto_ip);

if (slisten == socket_error)

//select the local inte***ce, and bind to it

struct sockaddr_in local;

if (binte***ce)

}else

local.sin_family = af_inet;

local.sin_port = htons(iport);

//繫結監聽網絡卡位址和埠

if (bind(slisten, (struct sockaddr*)&local, sizeof(local)) == socket_error)

//開始監聽客戶端

listen(slisten, 8);

printf("begin accept on port:%d\n", iport);

//in a continuous loop, wait for incoming clients.once one

//is detected, create a thread and pass the handle off to it

while (true)

printf("accepted client:%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));

dword dwthreadid;

handle hthread = createthread(null, 0, clientthread, (lpvoid)sclient,

0, &dwthreadid);

if (hthread == null)

closehandle(hthread);

}closesocket(slisten);

wsacleanup();

return 0;}

客戶端**,tcpclient.cpp。

//#include "stdafx.h"

#include #include #include #define default_count 5

#define default_port 5150

#define default_buffer 2048

#define default_message _t("this is a test of the emergency broadcasting system")

tchar szserver[128];

tchar szmessage[1024];

int iport = default_port;

dword dwcount = default_count;

bool bsendonly = false;

//print usage information and exit

void usage()

//parse the command line arguments, and set some global flags

//to indicate what actions to perform

void validateargs(int argc, _tchar** argv)

break;

case _t('s'):

if (_tcslen(argv[i]) > 3)

break;

case _t('n'):

if (_tcslen(argv[i]) > 3)

break;

case _t('o'):

bsendonly = true;

break;

default:

usage();

break;}}

}}int _tmain(int argc, _tchar* argv)

_tcscpy_s(szmessage, default_message);

//create the socket, and attempt to connect to the server

socket sclient = socket(af_inet, sock_stream, ipproto_ip);

if (sclient == invalid_socket)

struct sockaddr_in server;

server.sin_family = af_inet;

server.sin_port = htons(iport);

//if the supplied server address wasn't in the form

//"aaa.bbb.ccc.ddd,"it's a host name, so try to resolve it

int nserverlen = (int)_tcslen(szserver);

if (0 == nserverlen)

copymemory(&server.sin_addr, host->h_addr_list[0], host->h_length);

}else

if (connect(sclient, (struct sockaddr*)&server, sizeof(server))

== socket_error)

//send and receive data

char szbuffer[default_buffer];

int ret = 0;

for (int i = 0; i < (int)dwcount; i++)

else if (ret == socket_error)

printf("send %d bytes\n", ret);

if (!bsendonly)

else if (ret == socket_error)

szbuffer[ret] = _t('\0');

printf("recv [%d bytes]:'%s'\n", ret, szbuffer);}}

closesocket(sclient);

wsacleanup();

return 0;

}

TCP Socket 通訊學習開端

看了併發的go感覺自己又要加乙個內容了,主要是go涉及高併發 高併發涉及計算機網路 我不會計算機網路,我如果不會計網根本就無法觸及很多專案,但我network課下學期才上,這只好自己來學習了,被艹爛的內容應該不會是什麼特別難的內容吧 以下的 用的是別人現成的,時間緊迫,要先了解乙個東西,再去細究其原...

TCP Socket 通訊流程

流程說明 1 服務端建立乙個指定埠號的serversocket物件,呼叫accept 方法,等待接受客戶端的連線請求 等待期間當前程序處於阻塞狀態 2 客戶端建立乙個socket物件,指定服務點主機的ip位址 或網域名稱 和埠,發出tcp連線請求 3 服務端接受到客戶端的連線請求,程序由阻塞態變為執...

Tcp Socket非同步通訊例項

socket客戶端實體類,作用傳送和接收資料報 public class sockethelper catch argumentexception ae catch socketexception ex region methods socket通訊機連線函式 public bool connect...