|
1楼 //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(); } |
|
|
|
|
2楼 #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(); } |
|
|
|
|
3楼 端口 23 一般保留给 telnet 用的,最好 1024 以下的端口都不要用, unix 系统会要求 root 才能绑定的 (当然 Windows 不会有这个要求)。 建议用户可自己设置端口。 |
|
|
|
| 218.19.34.* |
5楼 在群发送时每一次sent都要Do event,刚开始不懂,每次只给最后一人发送成功。 真好的回忆... ... |
|
|
| 219.145.147.* |
6楼 |
|
|
| 220.184.252.* |
7楼 咱咋就是弄不明白C呢? |
|
|
| 219.221.105.* |
8楼 要做个一个SERVER能连很多CLIENT,用线程池做。 |
|
|
| 222.161.176.* |
9楼 |
|
|
| 218.64.126.* |
10楼
|
|
|
|
11楼 我在图书馆泡了不只一天了,就是从来没发现有这方面的,真是晕啊 |
|
|
| 202.204.120.* |
12楼 |
|
|
| 221.232.226.* |
13楼 |
|
|
| 219.154.94.* |
14楼 比我强多了 |
|
|
| 61.157.226.* |
15楼 简单哦 要是能自己根据协议写结构体,呵呵底层编程就可以了! |
|
|
| 222.212.27.* |
17楼 |
|
|
| 220.168.78.* |
18楼 这个是初始化socket吧,里面的0x101是代表了这个socket的版本吧,是不是1.1版本的,如果我们系统自带的版本比他要高,那怎么样才可以让程序自动获取系统的socket版本啊!望高手指教,还有就是本程序没有写出出错后怎么得到出错信息,以便于大家解决问题啊!!! |
|
|
| 220.188.48.* |
19楼 |
|
|
| 59.52.187.* |
20楼 同VC试下`` 我建议大家两种编译器都装`` |
|
|
| 219.140.55.* |
21楼 |
|
|
| 220.167.45.* |
22楼 |
|
|
| 218.104.50.* |
23楼 QQ:124541302 谢谢 |
|
|
| 61.185.224.* |
24楼 |
|
|
| 61.191.25.* |
25楼 Compiling... Cpp1.cpp Linking... Cpp1.obj : error LNK2001: unresolved external symbol __imp__WSACleanup@0 Cpp1.obj : error LNK2001: unresolved external symbol __imp__closesocket@4 Cpp1.obj : error LNK2001: unresolved external symbol __imp__recv@16 Cpp1.obj : error LNK2001: unresolved external symbol __imp__send@16 Cpp1.obj : error LNK2001: unresolved external symbol __imp__inet_ntoa@4 Cpp1.obj : error LNK2001: unresolved external symbol __imp__accept@12 Cpp1.obj : error LNK2001: unresolved external symbol __imp__listen@8 Cpp1.obj : error LNK2001: unresolved external symbol __imp__bind@12 Cpp1.obj : error LNK2001: unresolved external symbol __imp__socket@12 Cpp1.obj : error LNK2001: unresolved external symbol __imp__htons@4 Cpp1.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8 Debug/Cpp1.exe : fatal error LNK1120: 11 unresolved externals Error executing link.exe. Cpp1.exe - 12 error(s), 0 warning(s) |
|
|
| 218.64.1.* |
26楼 那位大哥,大姐帮帮我写一个 用C 语言编写的局域聊天的程序呀!!!!!! 如有请联系QQ285850350 |
|
|
| 218.64.1.* |
27楼 情况十分火急 |
|
|
| 219.133.252.* |
28楼 |
|
|

