其中有部分是處理資訊,臨時需要
#pragma once
標頭檔案/*
clinetnet.h:
用於表示乙個客戶的網路操作
*/#include #include#pragma comment(lib, "ws2_32.lib")
class cclientnet
;
客戶端的cpp
#include "clientnet.h"
#include #include using namespace std;
int cclientnet::connect( int port,const char* address )
//建立socket
m_sock = socket(af_inet,sock_stream,ipproto_tcp);
if (m_sock == invalid_socket)
//建立socket失敗
//目標伺服器資料
sockaddr_in sockaddrserver;
sockaddrserver.sin_family = af_inet;
sockaddrserver.sin_port = port;
sockaddrserver.sin_addr.s_addr = inet_addr(address);
//連線,sock與目標伺服器連線
ierrmsg = connect(m_sock,(sockaddr*)&sockaddrserver,sizeof(sockaddrserver));
if (ierrmsg < 0)
return rlt;
}socket cclientnet::getsocket()
int cclientnet::sendmsg(const char* msg,int len)
return rlt;
}void cclientnet::close()
void main()
//建立服務端socket
socket m_sock = socket(af_inet,sock_stream,ipproto_tcp);
if (m_sock == invalid_socket)
//宣告資訊
sockaddr_in serveraddr;
serveraddr.sin_family = af_inet;
serveraddr.sin_port = port;
serveraddr.sin_addr.s_addr = inet_addr(address);
//繫結
ierrormsg = bind(m_sock,(sockaddr*)&serveraddr,sizeof(serveraddr));
if (ierrormsg < 0)
listen(m_sock,5);
sockaddr_in tcpaddr;
int len =sizeof(sockaddr);
socket newsocket;
char buf[1024];
int rval;
newsocket = accept(m_sock,(sockaddr*)&tcpaddr,&len);
if(newsocket!=invalid_socket) }
//關閉對應accept的socket
closesocket(newsocket);}
int main(int argc, char **argv)
else
printf("servernet init failed with error : %d\n",irlt);
system("pause");
}
簡單的TCP通訊
server include include include include include int main int argc,const char argv int bind result bind server socket,struct sockaddr server addr,sizeof...
TCP簡單的通訊
客戶端 傳送端 include stdafx.h tcp通訊客戶端 include pragma comment lib,ws2 32.lib include pragma warning disable 4996 using namespace std const int port 9002 in...
python socket和簡單tcp通訊實現
python 服務端和客戶端的簡單互動 tcp服務端 1 建立套接字,繫結套接字到本地ip與埠 s socket.socket socket.af inet,socket.sock stream s.bind 2 開始監聽連線 s.listen 3 進入迴圈,不斷接受客戶端的連線請求 s.accep...