实例一;
客户端向服务器发信息, 收到后打印一下,再回发. 客户端收到信息后打印.【来自网络】
---------------这是服务器的-------------------
#include "afxmt.h"
#include "winsock2.h"
#pragma comment(lib,"WS2_32.LIB")
int bufi;
class mserver{
private:
SOCKET ssock,csock;
sockaddr_in saddr,caddr;
int len;
char buf[4096];
public:
mserver(){
}
mserver(int port,int maxclient){
saddr.sin_family=AF_INET;
saddr.sin_port=htons(port);
saddr.sin_addr.s_addr=INADDR_ANY;
ssock=socket(AF_INET,SOCK_STREAM,0);
if(ssock==INVALID_SOCKET){
printf("build socket error\n");
}
if(bind(ssock,(struct sockaddr*)&saddr,sizeof(saddr))==SOCKET_ERROR){
printf("socket bind fail\n");
}
len=sizeof(caddr);
}
void close(){
closesocket(ssock);
closesocket(csock);
}
void beginlisten(){
if(listen(ssock,10)==SOCKET_ERROR){
printf("listen fail\n");
}
}
void maccept(){
sockaddr a;
csock=accept(ssock,(struct sockaddr*)&caddr,&len);
if(csock==INVALID_SOCKET){
printf("accept fail : %d\n",WSAGetLastError());
}
printf("client from %s connected\n",inet_ntoa(caddr.sin_addr));
}
int receive(){
memset(buf,0,4096);
if(recv(csock,buf,4096,0)==SOCKET_ERROR){
return 0;
}
printf("message received : %s\n",buf);
send(csock,buf,4096,0);
return 1;
}
};
int main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,1),&wsaData)){
printf("Winsock initializing fail\n");
WSACleanup();
return 0;
}
mserver s(12345,10);
s.beginlisten();
s.maccept();
while(s.receive());
s.close();
WSACleanup();
return 0;
}
----------------这是客户端的-------------------
#include "stdio.h"
#include "afxmt.h"
#include "winsock2.h"
#pragma comment(lib,"WS2_32.LIB")
class mclient{
private:
SOCKET sock;
sockaddr_in saddr;
int len;
char buf[4096];
public:
mclient(){
sock=socket(AF_INET,SOCK_STREAM,0);
len=sizeof(saddr);
}
bool mconnect(char* addr,int port){
saddr.sin_family=AF_INET;
saddr.sin_port=htons(port);
saddr.sin_addr.s_addr=inet_addr(addr);
if(connect(sock,(struct sockaddr*)&saddr,len)==SOCKET_ERROR){
printf("connect fail : %d",WSAGetLastError());
return false;
}
return true;
}
void close(){
closesocket(sock);
}
void msend(char* mes){
send(sock,mes,strlen(mes),0);
}
bool receive(){
memset(buf,0,4096);
if(recv(sock,buf,4096,0)==SOCKET_ERROR){
return false;
}
else{
printf("received from server : %s\n",buf);
return true;
}
}
};
int main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,1),&wsaData)){
printf("Winsock initializing fail\n");
WSACleanup();
return 0;
}
mclient a;
if(!a.mconnect("172.18.48.7",12345)){
return 0;
}
char buf[1024];
while(scanf("%s",buf)){
if(strcmp(buf,"quit")==0){
break;
}
a.msend(buf);
a.receive();
}
a.close();
WSACleanup();
return 0;
}
实例2;
---------------这是服务器的-------------------
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define BUFLEN 1024
int main()
{
SOCKET serversoc;
SOCKET clientsoc;
SOCKADDR_IN serveraddr;
SOCKADDR_IN clientaddr;
char buf[BUFLEN];
int len;
WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa); //initial Ws2_32.dll by a process
if((serversoc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0) //create a tcp socket
{
printf("Create socket fail!\n");
return -1;
}
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(9102);
serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
if(bind(serversoc, (SOCKADDR *)&serveraddr, sizeof(serveraddr)) != 0)
{
printf("Bind fail!\n");
return -1;
}
//start listen, maximum length of the queue of pending connections is 1
printf("Start listen...\n");
if(listen(serversoc, 1) != 0)
{
printf("Listen fail!\n");
return -1;
}
len = sizeof(SOCKADDR_IN);
//waiting for connecting
if((clientsoc = accept(serversoc, (SOCKADDR *)&clientaddr, &len))<=0)
{
printf("Accept fail!\n");
return -1;
}
printf("Connected\n");
while(1)
{
//waiting for data receive
if(recv(clientsoc, buf, BUFLEN, 0) <= 0)
{
//some error occur
printf("Close connection\n");
closesocket(clientsoc);
break;
}
printf("%s\n",buf);
}
WSACleanup(); //clean up Ws2_32.dll
return 0;
}
----------------这是客户端的-------------------
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main()
{
SOCKET soc;
SOCKADDR_IN serveraddr;
SOCKADDR_IN clientaddr;
unsigned char buf[1024];
WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa); //initial Ws2_32.dll by a process
if((soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0) //create a tcp socket
{
printf("Create socket fail!\n");
return -1;
}
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(9102);
serveraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
//connect to server
printf("Try to connect...\n");
if(connect(soc, (SOCKADDR *)&serveraddr, sizeof(serveraddr)) != 0)
{
printf("Connect fail!\n");
return -1;
}
printf("Connected\n");
while(1)
{
scanf("%s", buf);
//send to server
if(send(soc, buf, strlen(buf)+1, 0)<=0)
{
printf("Error!\n");
}
}
WSACleanup(); //clean up Ws2_32.dll
return 0;
}