windows:
TCP通信:
//client
#include
#include
void main()
{
WORD wVersionRequested ;
WSADATA wsaData ;
int err;
wVersionRequested = MAKEWORD( 1, 1) ;
err = WSAStartup( wVersionRequested ,&wsaData );
if(err != 0)
{
return ;
}
if(LOBYTE(wsaData.wVersion ) !=1 ||HIBYTE( wsaData.wVersion ) != 1)
{
WSACleanup();
return ;
}
//创建套接字
SOCKET sockClient = socket(AF_INET , SOCK_STREAM ,0) ;
SOCKADDR_IN addrSrv ;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET ;
addrSrv.sin_port = htons(8000);
connect(sockClient ,(SOCKADDR*)&addrSrv ,sizeof(SOCKADDR ));
//接收数据;
char recvBuf[100];
recv(sockClient ,recvBuf,100,0) ;
printf("recv :%s",recvBuf) ;
send(sockClient ,"this is the client ",strlen("this is the client ") +1,0) ;
closesocket(sockClient) ;
WSACleanup ();
}
//server.cpp
#include
#include
void main()
{
//
WORD wVersionReguested ;
WSADATA wsaData ;
int err;
wVersionReguested = MAKEWORD( 1 ,1 );
err = WSAStartup(wVersionReguested ,&wsaData ) ;
if( err !=0 )
{
return ;
}
if(LOBYTE(wsaData.wVersion )!=1 || HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup() ;
return ;
}
SOCKET sockSrv = socket(AF_INET ,SOCK_STREAM, 0) ;
SOCKADDR_IN addrSrv ;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY) ;
addrSrv.sin_family = AF_INET ;
addrSrv.sin_port = htons(8000) ;
//绑定套接字
bind(sockSrv ,(SOCKADDR *)&addrSrv ,sizeof(SOCKADDR) );
listen(sockSrv,5) ;
SOCKADDR_IN addrClient ;
int len = sizeof(SOCKADDR) ;
while(1)
{
//等待客户请求的到来;
SOCKET sockConn = accept(sockSrv ,(SOCKADDR*)&addrClient ,&len );
char sendBuf[100];
sprintf(sendBuf,"welcom %s here !\n",inet_ntoa(addrClient.sin_addr));
//发送数据
send(sockConn ,sendBuf ,strlen(sendBuf)+1,0) ;
char recvBuf[100];
recv(sockConn, recvBuf,100,0);
printf("recv: %s\n",recvBuf) ;
closesocket(sockConn) ;
}
}
UDP通信
服务端:
#include
#include "winsock2.h"
void server() {
WSADATA wsaData;
SOCKET RecvSocket;
sockaddr_in RecvAddr;
int Port = 27015;
char RecvBuf[1024];
int BufLen = 1024;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
//-----------------------------------------------
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);
//-----------------------------------------------
// Create a receiver socket to receive datagrams
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//-----------------------------------------------
// Bind the socket to any address and the specified port.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));
//-----------------------------------------------
// Call the recvfrom function to receive datagrams
// on the bound socket.
printf("Receiving datagrams...\n");
while(1){
memset(RecvBuf,0,sizeof(RecvBuf)) ;
recvfrom(RecvSocket,
RecvBuf,
BufLen,
0,
(SOCKADDR *)&SenderAddr,
&SenderAddrSize);
printf("from:%s::%s\n",inet_ntoa(SenderAddr.sin_addr),RecvBuf);
}
//-----------------------------------------------
// Close the socket when finished receiving datagrams
printf("Finished receiving. Closing socket.\n");
closesocket(RecvSocket);
//-----------------------------------------------
// Clean up and exit.
printf("Exiting.\n");
WSACleanup();
return;
}
客户端:
void client() {
WSADATA wsaData;
SOCKET SendSocket;
sockaddr_in RecvAddr;
int Port = 27015;
char SendBuf[1024];
int BufLen = 1024;
//---------------------------------------------
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);
//---------------------------------------------
// Create a socket for sending data
SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//---------------------------------------------
// Set up the RecvAddr structure with the IP address of
// the receiver (in this example case "123.456.789.1")
// and the specified port number.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("10.33.28.230");
//---------------------------------------------
// Send a datagram to the receiver
printf("Sending a datagram to the receiver...\n");
while(1){
int n = scanf("%s",SendBuf);
int res = sendto(SendSocket,
SendBuf,
n,
0,
(SOCKADDR *) &RecvAddr,
sizeof(RecvAddr));
if(res==n){
printf("send OK\n");
}
}
//---------------------------------------------
// When the application is finished sending, close the socket.
printf("Finished sending. Closing socket.\n");
closesocket(SendSocket);
//---------------------------------------------
// Clean up and quit.
printf("Exiting.\n");
WSACleanup();
return;
}
linux版
UDP
服务器:
#include
#include /* See NOTES */
#include
#include
#include
#include
#include
#include
typedef struct sockaddr_in sockaddr_in ;
typedef struct sockaddr sockaddr ;
static inline int nonblock(int fd) {
int opt;
opt = fcntl(fd, F_GETFL);
opt |= O_NONBLOCK;
return fcntl(fd, F_SETFL, opt);
}
static inline int myblock(int fd) {
int opt;
opt = fcntl(fd, F_GETFL);
opt &= ~O_NONBLOCK;
return fcntl(fd, F_SETFL, opt);
}
int main(int argc ,char ** argv){
sockaddr_in srv ,cli ;
int sfd ;
if((sfd=socket(AF_INET ,SOCK_DGRAM,0))==-1){
perror("socket error:");
return -1 ;
}
if(argc < 2){
printf("please input argc with port num!");
return -1 ;
}
memset(&srv,0,sizeof(srv));
srv.sin_port = htons(atoi(argv[1]));
srv.sin_family = AF_INET ;
srv.sin_addr.s_addr = INADDR_ANY ;
int bfd = bind(sfd , (sockaddr*)&srv,sizeof(sockaddr)) ;
if(bfd==-1){
perror("bind error:");
return -1 ;
}
char buf[1024] ;
int len ,flags;
static int count ;
flags = MSG_WAITALL ;
while(1){
memset(buf,0,sizeof(buf));
len = sizeof(cli);
int rs,sum=0;
/* fcntl(sfd,F_SETFL,O_NONBLOCK);*/
flags = 0 ;
while((rs = recvfrom(sfd,buf,sizeof(buf)-1,flags,(sockaddr*)&cli,&len))>0){
printf("%s",buf);
sum+=rs ;
nonblock(sfd);
}
printf("\n");
myblock(sfd);
/*fcntl(sfd,F_SETFL,O_NONBLOCK);*/
printf("recv size:%d",sum);
if(rs<0 && errno!=EAGAIN){
perror("recvfrom:");
continue ;
}
printf("recv %d bytes from %s,contents:%s,count:=%d\n",
rs,inet_ntoa(cli.sin_addr),buf,count++);
}
close(sfd);
return 0;
}
客户端:
#include
#include /* See NOTES */
#include
#include
#include
#include
typedef struct sockaddr_in sockaddr_in ;
typedef struct sockaddr sockaddr ;
int main(int argc ,char ** argv){
sockaddr_in srv ,cli ;
int sfd ;
if((sfd=socket(AF_INET ,SOCK_DGRAM,0))==-1){
perror("socket error:");
return -1 ;
}
if(argc < 3){
printf("the argcs like : ./a.out ipaddr serverport ");
return -1 ;
}
memset(&srv,0,sizeof(srv));
srv.sin_port = htons(atoi(argv[2]));
srv.sin_family = AF_INET ;
inet_aton(argv[1],&srv.sin_addr);
char buf[1024] ;
int len,snu,re;
int64_t i ;
/* for(i=0;i<1000*1000*1000;i++){
memset(buf,0,sizeof(buf)) ;
sprintf(buf,"%d",i);
snu = sendto(sfd,buf,strlen(buf),0,
(sockaddr*)&srv,sizeof(sockaddr)) ;
}
*/
while(1){
bzero(buf,sizeof(buf));
re = read(STDIN_FILENO ,buf,sizeof(buf));
snu = sendto(sfd,buf,re-1,0,
(sockaddr*)&srv,sizeof(sockaddr)) ;
printf("send %d bytes\n",snu);
}
close(sfd);
return 0;
}
TCP
服务器:
#include
#include /* See NOTES */
#include
#include
#include
#include
#include
#include
typedef struct sockaddr_in sockaddr_in ;
typedef struct sockaddr sockaddr ;
static inline int nonblock(int fd) {
int opt;
opt = fcntl(fd, F_GETFL);
opt |= O_NONBLOCK;
return fcntl(fd, F_SETFL, opt);
}
int main(int argc ,char ** argv){
sockaddr_in srv ,cli ;
int sfd ,sfd2;
if((sfd=socket(AF_INET ,SOCK_STREAM,0))==-1){
perror("socket error:");
return -1 ;
}
if(argc < 2){
printf("please input argc with port num!");
return -1 ;
}
memset(&srv,0,sizeof(srv));
srv.sin_port = htons(atoi(argv[1]));
srv.sin_family = AF_INET ;
srv.sin_addr.s_addr = INADDR_ANY ;
int yes = 1;
if (-1 == setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int))) {
close(sfd);
return -1;
}
int bfd = bind(sfd , (sockaddr*)&srv,sizeof(sockaddr)) ;
if(bfd==-1){
perror("bind error:");
return -1 ;
}
listen(sfd, 20) ;
char buf[1024] ;
int len ;
while(1){
len = sizeof(sockaddr);
sfd2 = accept(sfd,(sockaddr*)&cli,&len);
printf("accept a sock\n");
memset(buf,0,sizeof(buf));
nonblock(sfd2);
while(1){
int rs ;
while(( rs = recv(sfd2,buf,20,MSG_WAITALL))>0){
printf("%s",buf) ;
}
if(rs<0&&errno==EAGAIN){
continue;
}
printf("over\n");
if(rs==0 || rs<0 )
break;
}
}
close(sfd);
return 0;
}
客户端:
#include
#include /* See NOTES */
#include
#include
#include
#include
typedef struct sockaddr_in sockaddr_in ;
typedef struct sockaddr sockaddr ;
int main(int argc ,char ** argv){
sockaddr_in srv ,cli ;
int sfd ;
if((sfd=socket(AF_INET ,SOCK_STREAM,0))==-1){
perror("socket error:");
return -1 ;
}
if(argc < 3){
printf("the argcs like : ./a.out ipaddr serverport ");
return -1 ;
}
memset(&srv,0,sizeof(srv));
srv.sin_port = htons(atoi(argv[2]));
srv.sin_family = AF_INET ;
inet_aton(argv[1],&srv.sin_addr);
char buf[1024] ;
int len,i ,snu,re;
int sfd2 = connect(sfd,(sockaddr*)&srv,sizeof(sockaddr));
if(sfd2 !=0){
perror("");
return -1;
}
while(1){
bzero(buf,sizeof(buf));
re = read(STDIN_FILENO ,buf,sizeof(buf));
snu = send(sfd,buf,re-1,0);
}
close(sfd);
return 0;
}