主题:socket 连接两台电脑
请求帮忙,
服务器与客户机相连,客户机可以向服务器发送,服务器不能向客户机发送,
请求帮忙解决服务器怎么向客户机发送
服务器代码:
[code=c]
请填写代码
[/code]
#include <winsock.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "passive_server.C"
#pragma comment(lib,"wsock32")
#define BUFSIZE 2048
#define WAVERS MAKEWORD(2,0)
SOCKET UDPserver(const char *service);
void main()
{
char *service="3334";
struct sockaddr_in sin;
int socket;
WSADATA wsadata;
int alen,cc;
char buf[BUFSIZE];
if(WSAStartup(WAVERS,&wsadata)!=0)
{
printf("initalize failed\n");
WSACleanup();
exit(1);
}
socket=UDPserver(service);
while(1)
{
alen=sizeof(struct sockaddr);
if((cc=recvfrom(socket,buf,sizeof(buf)-1,0,(struct sockaddr *)&sin,&alen))>0)
{
buf[cc]='\0';
printf("%s",buf);
}
}
closesocket(socket);
}[code=c]
请填写代码
[/code]
包含的函数:
[code=c]
请填写代码
[/code]#include <string.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
//sockserver
SOCKET sockserver(const char *service,const char *transport,int qlen)
{
struct sockaddr_in ipaddr;
struct servent *servin;
struct protoent *protoin;
int sock,type;
memset(&ipaddr,0,sizeof(ipaddr));
ipaddr.sin_family=AF_INET;
ipaddr.sin_addr.s_addr=INADDR_ANY;
if(servin=getservbyname(service,transport))
ipaddr.sin_port=htons((u_short)servin->s_port);
else if((ipaddr.sin_port=htons((u_short)atoi(service)))==0)
{
printf("get portnumber error \n");
WSACleanup();
exit(1);
}
if((protoin=getprotobyname(transport))==0)
{
printf("get protocol error \n");
WSACleanup();
exit(1);
}
if(strcmp(transport,"udp")==0)
type=SOCK_DGRAM;
else
type=SOCK_STREAM;
sock=socket(PF_INET,type,protoin->p_proto);
if(sock==INVALID_SOCKET)
{
printf("creat socket error \n");
WSACleanup();
exit(1);
}
if(bind(sock,(struct sockaddr *)&ipaddr,sizeof(ipaddr))==SOCKET_ERROR)
{
printf("socket bind error \n");
WSACleanup();
exit(1);
}
if(type==SOCK_STREAM)
{
if(listen(sock,qlen)==SOCKET_ERROR)
{
printf("socket listen error \n");
WSACleanup();
exit(1);
}
}
return sock;
}
//UDPserver
SOCKET UDPserver(const char *service)
{
return sockserver(service,"udp",0);
}
//TCPserver
SOCKET TCPserver(const char *service,int qlen)
{
return sockserver(service,"tcp",qlen);
}
客户机:
[code=c]
请填写代码
[/code]
#include <string.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include "connect_client.C"
#pragma comment(lib,"wsock32")
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
SOCKET UDPconnect(const char *,const char *);
void UDPtest(char *,char *);
SOCKET UDPserver(const char *service);
#define LINELEN 128
#define WAVERS MAKEWORD(2,0)
void main()
{
char *host="localhost";
char *service="3334";
WSADATA wsadata;
if(WSAStartup(WAVERS,&wsadata)!=0)
{
printf("initalize failed\n");
WSACleanup();
exit(1);
}
UDPtest(host,service);
socket=UDPserver(service);
while(1)
{
alen=sizeof(struct sockaddr);
if((cc=recvfrom(socket,buf,sizeof(buf)-1,0,(struct sockaddr *)&sin,&alen))>0)
{
buf[cc]='\0';
printf("%s",buf);
}
}
WSACleanup();
system("pause");
exit(0);
}
void UDPtest(char *host,char *service)
{
char buf[LINELEN+1];
SOCKET s;
int outchars;
s=UDPconnect(host,service);
//while(1)
// {
while(fgets(buf,sizeof(buf),stdin))
{
buf[LINELEN]='\0';
outchars=strlen(buf);
send(s,buf,outchars,0);
//if(buf[0]='\n')
//break;
}
//}
closesocket(s);
}
包含的函数
[code=c]
请填写代码
[/code]#include <stdlib.h>
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
//create a socket and connect server
//SOCKET
SOCKET sockconnect(const char *host,const char *service,const char *transport)
{
struct protoent *protoin;
struct sockaddr_in ipaddr;
struct hostent *hostin;
struct servent *servin;
int sock,type;
memset(&ipaddr,0,sizeof(ipaddr));
ipaddr.sin_family=AF_INET;
if(servin=getservbyname(service,transport))
ipaddr.sin_port=servin->s_port;
else if((ipaddr.sin_port=htons((u_short)atoi(service)))==0)
{
printf("get server information error \n");
WSACleanup();
exit(1);
}
if((protoin=getprotobyname(transport))==0)
{
printf("get protocol information error \n");
WSACleanup();
exit(1);
}
if(hostin=gethostbyname(host))
memcpy(&ipaddr.sin_addr,hostin->h_addr,hostin->h_length);
else if((ipaddr.sin_addr.s_addr=inet_addr(host))==INADDR_NONE)
{
printf("get host IP information error \n");
WSACleanup();
exit(1);
}
if(strcmp(transport,"udp")==0)
type=SOCK_DGRAM;
else
type=SOCK_STREAM;
sock=socket(PF_INET,type,protoin->p_proto);
if(sock==INVALID_SOCKET)
{
printf("creat socket error \n");
WSACleanup();
exit(1);
}
if(connect(sock,(struct sockaddr*)&ipaddr,sizeof(ipaddr))==SOCKET_ERROR)
{
printf("connect socket error! please start server first \n");
WSACleanup();
exit(1);
}
return sock;
}
//connect with UDP
SOCKET UDPconnect(const char *host, const char *service)
{
return sockconnect(host,service,"udp");
}
//connect with TCP
SOCKET TCPconnect(const char *host, const char *service)
{
return sockconnect(host,service,"tcp");
}
服务器与客户机相连,客户机可以向服务器发送,服务器不能向客户机发送,
请求帮忙解决服务器怎么向客户机发送
服务器代码:
[code=c]
请填写代码
[/code]
#include <winsock.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "passive_server.C"
#pragma comment(lib,"wsock32")
#define BUFSIZE 2048
#define WAVERS MAKEWORD(2,0)
SOCKET UDPserver(const char *service);
void main()
{
char *service="3334";
struct sockaddr_in sin;
int socket;
WSADATA wsadata;
int alen,cc;
char buf[BUFSIZE];
if(WSAStartup(WAVERS,&wsadata)!=0)
{
printf("initalize failed\n");
WSACleanup();
exit(1);
}
socket=UDPserver(service);
while(1)
{
alen=sizeof(struct sockaddr);
if((cc=recvfrom(socket,buf,sizeof(buf)-1,0,(struct sockaddr *)&sin,&alen))>0)
{
buf[cc]='\0';
printf("%s",buf);
}
}
closesocket(socket);
}[code=c]
请填写代码
[/code]
包含的函数:
[code=c]
请填写代码
[/code]#include <string.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
//sockserver
SOCKET sockserver(const char *service,const char *transport,int qlen)
{
struct sockaddr_in ipaddr;
struct servent *servin;
struct protoent *protoin;
int sock,type;
memset(&ipaddr,0,sizeof(ipaddr));
ipaddr.sin_family=AF_INET;
ipaddr.sin_addr.s_addr=INADDR_ANY;
if(servin=getservbyname(service,transport))
ipaddr.sin_port=htons((u_short)servin->s_port);
else if((ipaddr.sin_port=htons((u_short)atoi(service)))==0)
{
printf("get portnumber error \n");
WSACleanup();
exit(1);
}
if((protoin=getprotobyname(transport))==0)
{
printf("get protocol error \n");
WSACleanup();
exit(1);
}
if(strcmp(transport,"udp")==0)
type=SOCK_DGRAM;
else
type=SOCK_STREAM;
sock=socket(PF_INET,type,protoin->p_proto);
if(sock==INVALID_SOCKET)
{
printf("creat socket error \n");
WSACleanup();
exit(1);
}
if(bind(sock,(struct sockaddr *)&ipaddr,sizeof(ipaddr))==SOCKET_ERROR)
{
printf("socket bind error \n");
WSACleanup();
exit(1);
}
if(type==SOCK_STREAM)
{
if(listen(sock,qlen)==SOCKET_ERROR)
{
printf("socket listen error \n");
WSACleanup();
exit(1);
}
}
return sock;
}
//UDPserver
SOCKET UDPserver(const char *service)
{
return sockserver(service,"udp",0);
}
//TCPserver
SOCKET TCPserver(const char *service,int qlen)
{
return sockserver(service,"tcp",qlen);
}
客户机:
[code=c]
请填写代码
[/code]
#include <string.h>
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include "connect_client.C"
#pragma comment(lib,"wsock32")
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
SOCKET UDPconnect(const char *,const char *);
void UDPtest(char *,char *);
SOCKET UDPserver(const char *service);
#define LINELEN 128
#define WAVERS MAKEWORD(2,0)
void main()
{
char *host="localhost";
char *service="3334";
WSADATA wsadata;
if(WSAStartup(WAVERS,&wsadata)!=0)
{
printf("initalize failed\n");
WSACleanup();
exit(1);
}
UDPtest(host,service);
socket=UDPserver(service);
while(1)
{
alen=sizeof(struct sockaddr);
if((cc=recvfrom(socket,buf,sizeof(buf)-1,0,(struct sockaddr *)&sin,&alen))>0)
{
buf[cc]='\0';
printf("%s",buf);
}
}
WSACleanup();
system("pause");
exit(0);
}
void UDPtest(char *host,char *service)
{
char buf[LINELEN+1];
SOCKET s;
int outchars;
s=UDPconnect(host,service);
//while(1)
// {
while(fgets(buf,sizeof(buf),stdin))
{
buf[LINELEN]='\0';
outchars=strlen(buf);
send(s,buf,outchars,0);
//if(buf[0]='\n')
//break;
}
//}
closesocket(s);
}
包含的函数
[code=c]
请填写代码
[/code]#include <stdlib.h>
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
//create a socket and connect server
//SOCKET
SOCKET sockconnect(const char *host,const char *service,const char *transport)
{
struct protoent *protoin;
struct sockaddr_in ipaddr;
struct hostent *hostin;
struct servent *servin;
int sock,type;
memset(&ipaddr,0,sizeof(ipaddr));
ipaddr.sin_family=AF_INET;
if(servin=getservbyname(service,transport))
ipaddr.sin_port=servin->s_port;
else if((ipaddr.sin_port=htons((u_short)atoi(service)))==0)
{
printf("get server information error \n");
WSACleanup();
exit(1);
}
if((protoin=getprotobyname(transport))==0)
{
printf("get protocol information error \n");
WSACleanup();
exit(1);
}
if(hostin=gethostbyname(host))
memcpy(&ipaddr.sin_addr,hostin->h_addr,hostin->h_length);
else if((ipaddr.sin_addr.s_addr=inet_addr(host))==INADDR_NONE)
{
printf("get host IP information error \n");
WSACleanup();
exit(1);
}
if(strcmp(transport,"udp")==0)
type=SOCK_DGRAM;
else
type=SOCK_STREAM;
sock=socket(PF_INET,type,protoin->p_proto);
if(sock==INVALID_SOCKET)
{
printf("creat socket error \n");
WSACleanup();
exit(1);
}
if(connect(sock,(struct sockaddr*)&ipaddr,sizeof(ipaddr))==SOCKET_ERROR)
{
printf("connect socket error! please start server first \n");
WSACleanup();
exit(1);
}
return sock;
}
//connect with UDP
SOCKET UDPconnect(const char *host, const char *service)
{
return sockconnect(host,service,"udp");
}
//connect with TCP
SOCKET TCPconnect(const char *host, const char *service)
{
return sockconnect(host,service,"tcp");
}