包含两个程序,一个server 端,一个client端,那个c闲人在的话让他给我留言
//server.cpp
#include<stdio.h>
#include<winsock2.h>
#include<string.h>
#include<conio.h>
SOCKET server;
SOCKET client;
WSADATA WSAData;
int WSAreturn;
sockaddr_in local;
sockaddr_in from;
int fromlen = sizeof(from);
char information[50];
char welcome[]="欢迎您登陆YYJW主机!\n";
char receive[256];
char ip[]="您的IP是:";
char chat[256];
char c;
int i=0;
bool cansend=false;
int GetIp()
{
WSAreturn = WSAStartup(0x101,&WSAData);
if(WSAreturn)
{
printf("发生一个未知错误!\n");
return 0;
}
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons((u_short)23);
server = socket(AF_INET,SOCK_STREAM,0);
if(server==INVALID_SOCKET )
{
printf("无法创建套接字!\n");
return 0;
}
if(bind(server,(sockaddr *)&local,sizeof(local))!=0)
{
printf("无法绑定套接字!\n");
return 0;
}
if(listen(server,5)!=0)
{
printf("无法监听端口!\n");
return 0;
}
while(true)
{
printf("正在等待用户连接...\n");
client = accept(server,(struct sockaddr *)&from,&fromlen);
strcpy(information,inet_ntoa(from.sin_addr));
strcat(welcome,information);
printf("接受来自%s的连接...\n",inet_ntoa(from.sin_addr));
printf("正在发送欢迎消息...\n");
send(client,welcome,strlen(welcome),0);
//send(client,ip,strlen(ip),0);
//send(client,information,strlen(information),0);
printf("欢迎消息信息已经成功发送至客户端!\n");
while(1)
{
if(!cansend)
{
printf("正在等待对方发来消息...\n");
cansend=false;
recv(client,receive,sizeof(receive),0);
printf("*********接受到的消息***********\n");
printf("%s\n",receive);
printf("**************结束**************\n");
cansend=true;
}
else
{
printf("请输入下一条消息:");
scanf("%s\0",&chat);
send(client,chat,strlen(chat),0);
printf("\n已经成功发送出信息!\n");
cansend=false;
}
}
closesocket(client);
}
closesocket(server);
WSACleanup();
return 1;
}
void main()
{
int ok;
ok = GetIp();
if(ok==0)
printf("无法完成请求!\n");
getch();
} //client.cpp
#include<stdio.h>
#include<winsock2.h>
#include<string.h>
#include<conio.h>
SOCKET client;
WSADATA WSAData;
int WSAreturn;
sockaddr_in to;
char information[256];
char welcome[]="我已经登陆到你的主机!\n";
char receive[256];
char chatto[256];
char servername[256];
char chatfrom[256];
char c;
long result;
bool cansend=true;
int connection()
{
WSAreturn = WSAStartup(0x101,&WSAData);
if(WSAreturn)
{
printf("发生一个未知错误!\n");
return 0;
}
client = socket(AF_INET,SOCK_STREAM,0);
to.sin_family = AF_INET;
to.sin_port = htons(23);
while(true)
{
printf("请输入要登陆的IP:");
scanf("%s",&servername);
to.sin_addr.s_addr = inet_addr(servername);
printf("正在连接...\n");
result = connect(client,(struct sockaddr *)&to,sizeof(to));
if(SOCKET_ERROR==result)
{
// 连接失败
printf("对不起,无法连接到目标主机!\n");
WSACleanup();
return 0;
}
else
{
cansend=false;
recv(client,receive,sizeof(receive),0);
printf("*******欢迎消息消息*******\n");
printf("%s\n",receive);
printf("***********结束*********\n");
cansend=true;
while(1)
{
if(cansend)
{
printf("请输入下一条信息:");
scanf("%s\0",&chatto);
send(client,chatto,strlen(chatto),0);
printf("\n已经成功发送信息!\n");
cansend=false;
}
else
{
printf("正在等待对方发来消息...\n");
cansend=false;
recv(client,chatfrom,sizeof(chatfrom),0);
printf("*******接收到的消息*******\n");
printf("%s\n",chatfrom);
printf("***********结束*********\n");
cansend=true;
}
}
}
}
closesocket(client);
WSACleanup();
return 1;
}
void main()
{
connection();
getch();
}>>local.sin_port = htons((u_short)23);
端口 23 一般保留给 telnet 用的,最好 1024 以下的端口都不要用,
unix 系统会要求 root 才能绑定的 (当然 Windows 不会有这个要求)。
建议用户可自己设置端口。以前用Winsock在VB下做过类似的东西,
在群发送时每一次sent都要Do event,刚开始不懂,每次只给最后一人发送成功。
真好的回忆... ...
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |