当前位置:首页 » 操作系统 » linuxtcp代码

linuxtcp代码

发布时间: 2024-12-18 18:48:50

A. linux C 进行TCP网络连接,怎样设置连接超时时间

如果你确定,真的不需要等这么久,或者用户希望可以随时中上连接过程,那么一般是用 非阻塞模式来做的. 看看我的这段连接代码(节选),可以作为TCP连接的典范:

bool CRemoteLink::Connect()
{
OnDisconnected(); // 如果已经连接,则断开

if(!m_bUseProxy)
{

m_iConnStatus = SS_CONNECTING; // 正在连接状态
GNTRACE ("开始连接到远程服务器[%s][%ld]...\n", m_strip.c_str(), m_port);

// 建立套接字, 准备连接到服务器
m_socket = ::socket(AF_INET, SOCK_STREAM, 0);
if (socket < 0) {
if(m_pCallBack)
m_pCallBack->OnSocketError(SE_CREATE, MSG_SE_CREATE);
return false;
}

// 设为异步操作方式
unsigned long on = 1;
if (::ioctlsocket(m_socket, FIONBIO, &on) < 0) {
::closesocket(m_socket);
if(m_pCallBack)
m_pCallBack->OnSocketError(SE_CREATE, MSG_SE_CREATE);
return false;
}

sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(m_strip.c_str());
addr.sin_port = htons(m_port);

int rt;
rt = ::connect(m_socket, (sockaddr *) &addr, sizeof(addr));
if (rt == 0) {
OnConnected();
return true;
}

// ==================================================================
timeval to;

// 首先建立连接
fd_set wfds;
fd_set efds;
FD_ZERO(&wfds);
FD_ZERO(&efds);

// test shutdown event each 100ms.
to.tv_sec = 0;
// CONNECT_TIMEOUT;
to.tv_usec = 100000;

int it = 0;
while(!m_meShutdown.Wait(0) && !m_meConnStop.Wait(0))
{
FD_SET(m_socket, &wfds);
FD_SET(m_socket, &efds);
int n = select(m_socket + 1, NULL, &wfds, &efds, &to);

if (n > 0) {
if(FD_ISSET(m_socket, &wfds))
{
OnConnected();
return true;
}
else
{
//int err = ::WSAGetLastError();
//const char* msg = GetLastErrorMessage(err);
GNTRACE ("CRemoteLink::Connect : connection attempt failed!\n");
if(m_pCallBack)
m_pCallBack->OnSocketError(SE_CONN, MSG_SE_CONN);
break;
}
} else if (n < 0) { // Select Error
int err = ::WSAGetLastError();
const char* msg = GetLastErrorMessage(err);
GNTRACE ("CRemoteLink::Connect : Select Error.[%d] - %s\n", err, msg);
if(m_pCallBack)
m_pCallBack->OnSocketError(err, msg);
break;
}
else
{
it += 100;
if(it > 30000) // 连接超时 -- (30S)
{
GNTRACE ("CRemoteLink::Connect : Time out.\n");
if(m_pCallBack)
m_pCallBack->OnSocketError(SE_TIMEOUT, MSG_SE_TIMEOUT);
break;
}
}
}
if(m_meConnStop.Wait(0))
{
GNTRACE("连接过程进行时被取消。\n");
}
}
else
{
// 通过代理服务器连接

B. linux shell 鑴氭湰瀹炵幇tcp/upd鍗忚阃氲

linux 璁惧囬噷闱㈡湁涓姣旇缉鐗规畩镄勬枃浠:

/dev/[tcp|upd]/host/port 鍙瑕佽诲彇鎴栬呭啓鍏ヨ繖涓鏂囦欢锛岀浉褰扑簬绯荤粺浼氩皾璇曡繛鎺:host 杩椤彴链哄櫒锛屽瑰簲port绔鍙c傚傛灉涓绘満浠ュ强绔鍙e瓨鍦锛屽氨寤虹珛涓涓狲ocket 杩炴帴銆傚皢鍦锛/proc/self/fd鐩褰曚笅闱锛屾湁瀵瑰簲镄勬枃浠跺嚭鐜般

涓銆佹祴璇曚笅锛/dev/tcp/host/post鏂囦欢

澶嶅埗浠g爜

   

浠g爜濡备笅:

[chengmo@centos5 shell]$ cat/dev/tcp/127.0.0.1/22

   SSH-2.0-OpenSSH_5.1

   #鎴戠殑链哄櫒shell绔鍙f槸锛22

   #瀹为檯:/dev/tcp镙规湰娌℃湁杩欎釜鐩褰曪纴杩欐槸灞炰簬鐗规畩璁惧

   [chengmo@centos5 shell]$ cat/dev/tcp/127.0.0.1/223

   -bash: connect: 𨰾掔粷杩炴帴

   -bash: /dev/tcp/127.0.0.1/223: 𨰾掔粷杩炴帴

   #223鎺ュ彛涓嶅瓨鍦,镓揿紑澶辫触

   [chengmo@centos5 shell]$ exec 8/dev/tcp/127.0.0.1/22

   [chengmo@centos5 shell]$ ls -l /proc/self/fd/

   镐昏 0

   lrwx------ 1 chengmo chengmo 64 10-21 23:05 0 - /dev/pts/0

   lrwx------ 1 chengmo chengmo 64 10-21 23:05 1 - /dev/pts/0

   lrwx------ 1 chengmo chengmo 64 10-21 23:05 2 - /dev/pts/0

   lr-x------ 1 chengmo chengmo 64 10-21 23:05 3 - /proc/22185/fd

   lrwx------ 1 chengmo chengmo 64 10-21 23:05 8 - socket:[15067661]

   #鏂囦欢鎻忚堪绗8锛屽凡缁忔墦寮涓涓狲ocket阃氲阃氶亾锛岃繖涓鏄涓涓鍙浠ヨ诲啓socket阃氶亾,锲犱负鐢锛""镓揿紑

   [chengmo@centos5 shell]$ exec 8-

   #鍏抽棴阃氶亾

   [chengmo@centos5 shell]$ ls -l /proc/self/fd/

   镐昏 0

   lrwx------ 1 chengmo chengmo 64 10-21 23:08 0 - /dev/pts/0

   lrwx------ 1 chengmo chengmo 64 10-21 23:08 1 - /dev/pts/0

   lrwx------ 1 chengmo chengmo 64 10-21 23:08 2 - /dev/pts/0

   lr-x------ 1 chengmo chengmo 64 10-21 23:08 3 - /proc/22234/fd

浜屻侀氲繃閲嶅畾钖戣诲彇杩灭▼web链嶅姟鍣ㄥご淇℃伅

澶嶅埗浠g爜

   

浠g爜濡备笅:

#!/bin/sh

   #testhttphead.sh

   #瀹炵幇阃氲繃涓绘満钖嶏纴绔鍙h诲彇web 链嶅姟鍣╤eader淇℃伅

   #right chengmo,qq:8292669

   if(($#2));then

   echo "usage:$0 host port";

   exit 1;

   fi

   #濡傛灉鍙傛暟缂哄け锛岄鍑虹▼搴忥纴杩斿洖鐘舵1

   exec 6/dev/tcp/$1/$2 2/dev/null;

   #镓揿紑host镄刾ort 鍙璇诲啓镄剆ocket杩炴帴锛屼笌鏂囦欢鎻忚堪绗6杩炴帴

   if(($?!=0));then

   echo "open $1 $2 error!";

   exit 1;

   fi

   #濡傛灉镓揿紑澶辫触锛$?杩斿洖涓崭负0锛岀粓姝㈢▼搴

   echo -e "HEAD / HTTP/1.1/n/n/n/n/n"6;

   #灏咹EAD 淇℃伅锛屽彂阃佺粰socket杩炴帴

   cat6;

   #浠巗ocket璇诲彇杩斿洖淇℃伅锛屾樉绀轰负镙囧嗳杈揿嚭

   exec 6-;

   exec 6-;

   #鍏抽棴socket镄勮緭鍏ワ纴杈揿嚭

   exit 0;

鑴氭湰寤虹珛钖庯细瀛树负testhttphead.sh

杩愯岀粨鏋滐细

澶嶅埗浠g爜

   

浠g爜濡备笅:

[chengmo@centos5 ~/shell]$ sh testhttphead.sh www..com 80

   HTTP/1.1 200 OK

   Date: Thu, 21 Oct 2010 15:17:23 GMT

   Server: BWS/1.0

   Content-Length: 6218

   Content-Type: text/html;charset=gb2312

   Cache-Control: private

   Expires: Thu, 21 Oct 2010 15:17:23 GMT

   Set-Cookie: BAIDUID=:FG=1; expires=Thu, 21-Oct-40 15:17:23 GMT; path=/; domain=..com

   P3P: CP=" OTI DSP COR IVA OUR IND COM "

   Connection: Keep-Alive

   [chengmo@centos5 ~/shell]$ sh testhttphead.sh 127.0.0.1 8080

   open 127.0.0.1 8080 error!

绐佺劧链変釜濂囨𨱍虫硶锛

鎴戜滑鍦╳indows镞朵唬灏遍氲繃telnet 鍙浠ュ疄鐜皌cp/upd鍗忚阃氲锛岄偅涔埚傛灉鐢ㄤ紶缁熸柟娉曟庝箞瀹炵幇锻锛

澶嶅埗浠g爜

   

浠g爜濡备笅:

[chengmo@centos5 ~/shell]$ echo -e "HEAD / HTTP/1.1/n/n/n/n/n"|telnet www..com 80

   Trying 220.181.6.175...

   Connected to www..com.

   Escape character is '^]'.

   Connection closed by foreign host.

   #鐩存帴缁椤彂阃侊纴澶辫触

   [chengmo@centos5 ~/shell]$ (telnet www..com 80)EOF

   HEAD / HTTP/1.1

   EOF

   Trying 220.181.6.175...

   Connected to www..com.

   Escape character is '^]'.

   Connection closed by foreign host.

   #閲嶅畾钖戣緭鍏ワ纴杩樻槸澶辫触锛

镓惧埌姝g‘鏂规硶锛

澶嶅埗浠g爜

   

浠g爜濡备笅:

[chengmo@centos5 shell]$ (echo -e "HEAD / HTTP/1.1/n/n/n/n/n";sleep 2)|telnet www..com 80

   Trying 220.181.6.175...

   Connected to www..com.

   Escape character is '^]'.

   HTTP/1.1 200 OK

   Date: Thu, 21 Oct 2010 15:51:58 GMT

   Server: BWS/1.0

   Content-Length: 6218

   Content-Type: text/html;charset=gb2312

   Cache-Control: private

   Expires: Thu, 21 Oct 2010 15:51:58 GMT

   Set-Cookie: BAIDUID=:FG=1; expires=Thu, 21-Oct-40 15:51:58 GMT; path=/; domain=..com

   P3P: CP=" OTI DSP COR IVA OUR IND COM "

   Connection: Keep-Alive

   #鎴愬姛浜嗭紒锷犲叆sleep 灞呯劧鍙浠ヤ简锛宻leep 鏀规垚1绉掍篃鍙浠

鏄涓嶆槸鐢变簬sleep钖庯纴echo浼氭帹鍑2绉掑彂缁欓氶亾锛歵elnet锻锛熸帹璁哄彲浠ヤ粠杩2涓鏂归溃鎺ㄧ炕锛

涓涓鏂归溃锛氶氲繃()𨰾镄勬暟鎹鏄涓瀵瑰懡浠わ纴浼氢綔涓轰竴涓瀛愬懡浠ゆ墽琛岋纴涓璧锋墽琛屽畬绋嫔簭缁撴潫銆傛疮涓锻戒护echo璇鍙ワ纴灏辩洿鎺ュ彂阃佸埌灞忓箷锛堜篃灏辨槸镙囧嗳杈揿嚭锛夛纴鍙瑕佹湁镙囧嗳杈揿嚭浜嗭纴灏变细阃氲繃阃氶亾椹涓娄紶涓锛歵elnet 锛屽傛灉鎺ヤ笅𨱒ュ懡浠よ缮链夎緭鍑猴纴浼氭敞镒忎紶缁檛elnet 锛岀洿鍒()鍐呮墍链夊懡浠ゆ墽琛屽畬锛屼笌阃氶亾杩炴帴灏辨柇寮浜嗐

鍐崭竴涓鏂归溃锛氩傛灉璇存槸璧峰埌鎺ㄨ繜鍙戦佺殑璇濓纴浠涔堟椂鍊欐湁鏁版嵁杩囨潵锛屽彂缁檛elnet锛屼粈涔堟椂鍊檛elnet锻戒护钖锷ㄣ傝窡浣犳帹杩熶竴镣硅缮鏄镞╀竴镣瑰彂阃佽繃𨱒ャ傛病链夊叧绯汇

杩欑岖被鍨嫔懡浠わ纴鐪嫔嚭sleep锛屽叾瀹炲氨鏄淇濇寔阃氶亾璺焧elnet 杩炴帴2绉挜挓銆 阃氶亾杩炴帴镌浜嗭纴telnet缁堢杈揿叆涔熻缮鍦锛屽洜姝ゅ彲浠ヤ缭鎸佷粠链嶅姟鍣ㄨ幏寰楁暟鎹銆

镓浠,寤惰繜澶氢箙锛岃缮鏄璺熸湇锷″櫒澶勭悊阃熷害链夊叧绯汇

濡傛灉阃氲繃echo 钖憈elnet鍙戦佹暟鎹锛屼缭鎸侀氶亾镵旈氾纴浣跨敤sleep鏄涓寰埚ソ鏂规硶銆

阃氲繃閲嶅畾钖戠粰telnet杈揿叆鍙傛暟杩欑嶆柟娉曪纴鎴戣缮𨱍充笉鍒版庝箞镙峰疄鐜板欢杩熻緭鍏ャ傛湁鐭ラ亾链嫔弸锛屽彲浠ユ寚镣规寚镣.

鍖哄埆锛

telnet涓巈cho 瀹炵幇 http璁块梾锛屼笌阃氲繃镓揿紑璇诲啓socket鏄涓崭竴镙风殑锛屾墦寮socket阃氶亾锛屾槸鍙浠ヨ繘琛屼氦鎹㈠勭悊镄勚备紶鍏ュ懡浠わ纴娲诲姩缁撴灉锛屽啀浼犲叆锻戒护锛屽啀銮峰缑缁撴灉銆伥elnet阃氲繃echo 灏变笉鑳借繖镙峰勭悊浜嗐

涓夈侀氲繃shell鑴氭湰閲嶅畾钖戝疄鐜扮洃鎺memcache鐘舵

澶嶅埗浠g爜

   

浠g爜濡备笅:

#!/bin/sh

   #阃氲繃浼犲叆ip 浠ュ强绔鍙o纴鍙戦佹寚浠よ幏寰楄繑锲炴暟鎹

   #right chengmo qq:8292669

   #鍑芥暟寰寰鏀惧埌链涓婇溃

   function sendmsg()

   {

   msg=$1;

   echo "$1"8;

   getout;

   }

   #钖憇ocket阃氶亾鍙戦佹寚浠わ纴骞朵笖璋幂敤銮峰缑杩斿洖鍙傛暟

   function getout()

   {

   #read 锻戒护 -u 浠庢墦寮鏂囦欢鎻忚堪绗 8 璇诲彇鏁版嵁锛-d璇诲彇鏁版嵁蹇界暐鎺:/r鎹㈣岀

   while read -u 8 -d $'/r' name;

   do

   if [ "${name}" == "END" -o "${name}" == "ERROR" ];then

   break;

   fi;

   echo $name;

   done

   }

   #鐢变簬锛歮emcached姣忔¢氲瀹屾瘯锛屼细杩斿洖锛欵ND鎴栬匛RROR(鍑洪敊锛夛纴阃氲繃鍒ゆ柇鏄钖︽槸"END"瑙夊缑璇诲彇鏄涓嶆槸缁撴潫锛屽惁鍒椤惊鐜涓崭细锅沧

   if [ $# -lt 2 ];then

   echo "usage:$0 host port [command]";

   exit 1;

   fi;

   [[ $# -gt 2 ]]command=$3;

   #璁剧疆榛樿ゅ 濡傛灉command涓哄畾涔夊垯涓猴细stats

   command="${command=stats}";

   host="$1";

   port="$2";

   exec 8/dev/tcp/${host}/${port};

   #镓揿紑阃氩悜阃氶亾鏄8

   if [ "$?" != "0" ];then

   echo "open $host $port fail!";

   exit 1;

   fi

   sendmsg "$command";

   #鍙戦佹寚瀹氩懡浠

   sendmsg "quit";

   #鍙戦侀鍑洪氩悜锻戒护

   exec 8-;

   exec 8-;

   #鍏抽棴socket阃氶亾

   exit 0;

杩欐槸阃氲繃閲嶅畾钖戯纴瀹炵幇socket阃氲涓锛屽彂阃佺劧钖庤幏鍙栬繑锲炵殑渚嫔瓙銆傚叾瀹烇纴涓婇溃浠g爜鐪嬩技涓娆″彧鑳藉彂阃佷竴娈点傛椂闂翠笂銆傛垜浠鍙浠ュ弽澶嶈皟鐢锛歴endmsg 锛屾崟鎹夎緭鍑烘暟鎹銆傚疄鐜拌繛缁镄勶纴璇讳笌鍐欐搷浣溿

瀹炰緥鎴锲撅细

鍏跺畠瀹炵幇鏂规硶锛

鍏跺疄阃氲繃锛歵elnet涔熷彲浠ュ疄鐜扮殑銆

[chengmo@centos5 shell]$ (echo "stats";sleep 2)|telnet 127.0.0.1 11211

阃氲繃nc锻戒护瀹炵幇锛

[chengmo@centos5 shell]$ (echo "stats")|nc 127.0.0.1 11211

涓嶉渶瑕佸姞寤惰繜锛岀洿鎺ユ墦寮阃氶亾

绗浜屼釜绋嫔簭閲岄溃锛岀湅鍒皊hell瀹屽叏鍙浠ュ勭悊浜や簰璁捐′简銆傚傛灉鎸夌収杩欐牱锛岀橱闄唂tp,pop3,stmp閮藉彲浠ョ被浼煎疄鐜般傝繖浜涳纴鎴戜滑阃氲繃shell socket绫讳技绋嫔簭瀹炵幇锛屽簲璇ヤ笉锲伴毦锛屽彧鏄鎹曟崏濡傚彂阃佽В鏋愮殑闂棰树简銆

C. LinuxC编程建立TCP连接linuxctcp

Linux C编程:建立 TCP连接
Linux C编程中使用TCP(Transmission Control Protocol,传输控制协议)协议建立客户端和服务器之间连接的过程称之为TCP连接,是一种可靠而强大的通信协议,在Linux C编程中可用于建立数据库、网络通信等等。本文介绍了在Linux C编程中如何建立TCP连接,以及其中遇到的一些问题。
在Linux C语言编程中,可以使用socket()函数建立一个TCP连接。socket()函数的第一个参数指定协议族,例如AF_INET指定IPV4协议族,第二个参数指定套接字类型,例如SOCK_STREAM指定流式套接字。
接下来,可以使用bind()函数将套接字与系统分配的IP地址和端口绑定,然后使用listen()函数使套接字变为被动模式,并启动监听进程,此时服务器已准备就绪,等待客户端的连接。最后,使用accept()函数接受客户端的连接,当接受到客户端的连接后,服务器就可以使用建立的socket与客户端通信了。
示例代码如下:
// 创建 socket
int sockfd;
struct sockaddr_in addr;
// AF_INET: IPV4 协议族
// SOCK_STREAM: 流式套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// 设置 IP 地址
addr.sin_family = AF_INET;
addr.sin_port = htons(9999); //端口号
addr.sin_addr.s_addr = inet_addr(“127.0.0.1”); //IP地址
// 绑定 IP 和 端口
bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
// 监听客户端请求
listen(sockfd, 20);
// 接受 客户端连接请求
struct sockaddr_in client_addr;
socklen_t client_addr_len;
int client_fd = accept(sockfd, (struct sockaddr*)&client_addr,
&client_addr_len);
上述步骤完成后,客户端和服务器的TCP连接建立完毕。在Linux C编程中,使用TCP协议建立客户端和服务器之间连接过程虽然繁琐,但是它可以实现可靠的数据传输和优秀的网络通信,这个代价值得支付。
总而言之,在Linux C编程中使用TCP协议建立客户端和服务器之间连接,可以通过socket()、bind()、listen()、accept()等函数将客户端和服务器建立可靠的数据传输连接,这是一个蛮耗时的过程,但也值得支付,因为通过这种方式可以实现稳定的网络通信。

热点内容
苹果跑分和安卓跑分有什么区别 发布:2024-12-18 21:47:41 浏览:504
c语言好考吗 发布:2024-12-18 21:43:20 浏览:290
陌陌密码忘了手机号也换了怎么办 发布:2024-12-18 21:21:03 浏览:190
轩逸建议买什么配置 发布:2024-12-18 21:19:29 浏览:192
手工反编译 发布:2024-12-18 21:14:39 浏览:901
后台ui源码 发布:2024-12-18 21:01:14 浏览:215
脚本刷曹仁 发布:2024-12-18 21:00:15 浏览:758
c语言是怎么实现跨平台编译的 发布:2024-12-18 20:55:20 浏览:996
电脑服务器机箱定做 发布:2024-12-18 20:51:03 浏览:902
电子商务系统数据库 发布:2024-12-18 20:24:06 浏览:329