windows 下 c语言,不是c++,c语言socket的简单实例
需要一个vc++6.0中能运行的socket的实例,包括server.c和client.c,谢谢啊...
需要一个vc++6.0中能运行的socket的实例,包括server.c和client.c,谢谢啊
展开
展开全部
服务器:
#include<stdio.h>
#include<winsock.h>
#pragma comment (lib,"wsock32.lib")
void main()
{
WSADATA data;
WSAStartup(MAKEWORD(2,0),&data);
SOCKET socket1;
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen =sizeof(from);
local.sin_family=AF_INET;
local.sin_port=htons(1000); ///监听端口
local.sin_addr.s_addr=INADDR_ANY; ///本机
socket1=socket(AF_INET,SOCK_DGRAM,0);
bind(socket1,(struct sockaddr*)&local,sizeof local);
while (1)
{
char buffer[1024]="\0";
printf("waiting for message from others-------------\n");
if (recvfrom(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,&fromlen)!=SOCKET_ERROR)
{
printf("Received datagram from %s--%s\n",inet_ntoa(from.sin_addr),buffer);
////给cilent发信息
sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,fromlen);
}
}
closesocket(socket1);
WSACleanup();
}
客户端:
#include<stdio.h>
#include<winsock.h>
#pragma comment (lib,"wsock32.lib")
void main()
{
WSADATA data;
WSAStartup(MAKEWORD(2,0),&data);
SOCKET socket1;
struct sockaddr_in server;
int len =sizeof(server);
server.sin_family=AF_INET;
server.sin_port=htons(1000); ///server的监听端口
server.sin_addr.s_addr=inet_addr("???.???.???.???"); ///server的地址
socket1=socket(AF_INET,SOCK_DGRAM,0);
while (1)
{
char buffer[1024]="\0";
printf("input message\n");
scanf("%s",buffer);
if (strcmp(buffer,"bye")==0)
break;
if (sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&server,len)!=SOCKET_ERROR)
{
if (recvfrom(socket1,
buffer,
sizeof buffer,
0,
(struct sockaddr*)&server,&len) != SOCKET_ERROR)
printf("rece from server:%s\n",buffer);
}
}
closesocket(socket1);
WSACleanup();
}
#include<stdio.h>
#include<winsock.h>
#pragma comment (lib,"wsock32.lib")
void main()
{
WSADATA data;
WSAStartup(MAKEWORD(2,0),&data);
SOCKET socket1;
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen =sizeof(from);
local.sin_family=AF_INET;
local.sin_port=htons(1000); ///监听端口
local.sin_addr.s_addr=INADDR_ANY; ///本机
socket1=socket(AF_INET,SOCK_DGRAM,0);
bind(socket1,(struct sockaddr*)&local,sizeof local);
while (1)
{
char buffer[1024]="\0";
printf("waiting for message from others-------------\n");
if (recvfrom(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,&fromlen)!=SOCKET_ERROR)
{
printf("Received datagram from %s--%s\n",inet_ntoa(from.sin_addr),buffer);
////给cilent发信息
sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,fromlen);
}
}
closesocket(socket1);
WSACleanup();
}
客户端:
#include<stdio.h>
#include<winsock.h>
#pragma comment (lib,"wsock32.lib")
void main()
{
WSADATA data;
WSAStartup(MAKEWORD(2,0),&data);
SOCKET socket1;
struct sockaddr_in server;
int len =sizeof(server);
server.sin_family=AF_INET;
server.sin_port=htons(1000); ///server的监听端口
server.sin_addr.s_addr=inet_addr("???.???.???.???"); ///server的地址
socket1=socket(AF_INET,SOCK_DGRAM,0);
while (1)
{
char buffer[1024]="\0";
printf("input message\n");
scanf("%s",buffer);
if (strcmp(buffer,"bye")==0)
break;
if (sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&server,len)!=SOCKET_ERROR)
{
if (recvfrom(socket1,
buffer,
sizeof buffer,
0,
(struct sockaddr*)&server,&len) != SOCKET_ERROR)
printf("rece from server:%s\n",buffer);
}
}
closesocket(socket1);
WSACleanup();
}
更多追问追答
追问
谢谢大神啦,但是编译出下面的问题,是哪里没设置对么?
c:\program files\microsoft visual studio\myprojects\server\server.c(8) : error C2275: 'SOCKET' : illegal use of this type as an expression
追答
我在vc++6.0都可以通过,你应该得使用.c++作为它的后缀名称,则能够顺利编过
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询