資源簡介
本程序通過在VC++6.0下用winsocket實現了數據通信,功能類似QQ應用程序,同時在服務端可接受來自客戶端上傳的圖像的資源。

代碼片段和文件信息
#include???
#include?
#include?“sock_file_recv_send.h“?
#pragma?comment(?lib?“WSOCK32.LIB“?)
//#include??
//#pragma?comment(?lib?“WS2_32.lib“?)???//很多人就是因為這個編譯不成功?:)
#define?PORT?8888
#define?Hostname??“127.0.0.1“
int?main()??
{
???FILE?*fp=NULL;?
???int?flag=0;
???char?msg_rec[20]filename[20]={““};;
???int?WSA_returnchld_socket;
???WSADATA?WSAData;
???HOSTENT?*host;??
???//struct?hostent?*host;
???WORD??wVersionRequested;
???struct?sockaddr_in?sa;
???wVersionRequested?=?MAKEWORD(?2?0?);
???WSA_return=WSAStartup(wVersionRequested&WSAData);?//進行WSAStartup函數調用
?
???if(WSA_return==0)????//???如果成功
?????printf(“Socket?inital?OK?!!\n“);
???else
?????printf(“ERROR\n“);???//錯誤提示????///*/
// while(1)
???{
?if((chld_socket=socket(AF_INETSOCK_STREAM0?))==-1)????//創建客戶端socket
?{
??????perror(“socket?client?creat?error?!!\n“);
??????exit(1);
?}
?????
?if((host=gethostbyname(Hostname))==NULL)
?{
??printf(“gethostbyname出錯!“);
??exit(1);
?}
sa.sin_family?=?AF_INET;
sa.sin_port?=?htons(PORT);??????//port?number
sa.sin_addr?=?*((struct?in_addr?*)host->h_addr);//htonl(INADDR_ANY);//address
????memset(&(sa.sin_zero)08);
???if(flag==0)
???{const?char?*pszAddr=inet_ntoa?(sa.sin_addr);?\
????printf(“the?host?IP?is:?%s\n“pszAddr);??//return?1;
???}
??????
if?(connect(chld_socket?(struct?sockaddr?*)&sa?\
????sizeof(struct?sockaddr))?==?-1)?
{
??????perror(“connect?error?.....\n“);
? ??return?1;
}
printf(“input?the?file?name?:\n?????“);????//輸入文件名
gets(filename);
????if((fp=fopen(“filename““rb“))!=NULL)
{
jpg_send(fpchld_socket);?????????//?發送圖片
}
else
{printf(“there?is?no?such?a?file?as?\“?%s?\“!!\n“filename);
?send(chld_socket?“error?!“sizeof(“error?!“)?0);
?goto?ppp;
}
/*///////////////////////SEND////////////////////////////////////
??printf(“(input?the?words?you?want?to?say:)\n“);
??????if(scanf(“%s“msg_rec))???
????????????if?(send(chld_socket?msg_recsizeof(msg_rec)?0)?==?-1)
?????????????{perror(“send?error!!“);?closesocket(chld_socket);exit(0);};
?????????????printf(“I?say:\n???%s\n“msg_rec);
??????if(!strcmp(msg_rec“end“))??{closesocket(chld_socket);break;};
////////////////////RECEVE//////////////////////////////////////
printf(“(wait?for?TA?say?...)\n“);
if?((recv(chld_socket?msg_rec?15?0))?==-1)?
???????{
????????perror(“recv出錯!“);closesocket(chld_socket);
???????exit(1);
???????//continue;
???????}
else
??????printf(“TA?say:\n???%s\n“msg_rec);
////////////////////////////////////////////////////////////////*/
fclose(fp);
ppp: closesocket(chld_socket);
}
WSACleanup();
return?0;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????528??2011-03-16?20:19??win_cilent\win_cilent.dsw
?????文件??????41984??2011-03-18?08:54??win_cilent\win_cilent.ncb
?????文件?????148480??2011-03-18?08:53??win_cilent\Debug\vc60.idb
?????文件??????77824??2011-03-18?08:46??win_cilent\Debug\vc60.pdb
?????文件?????180317??2011-03-18?08:46??win_cilent\Debug\win_cilent.exe
?????文件?????492544??2011-03-18?08:46??win_cilent\Debug\win_cilent.pdb
?????文件?????221636??2011-03-18?08:46??win_cilent\Debug\win_cilent.ilk
?????文件??????12510??2011-03-18?08:21??win_cilent\Debug\sock_file_recv_send.obj
?????文件????3569596??2011-03-18?08:28??win_cilent\Debug\win_cilent.pch
?????文件??????14988??2011-03-18?08:46??win_cilent\Debug\cilent.obj
?????文件???????1482??2011-03-18?08:46??win_cilent\win_cilent.plg
?????文件???????2626??2011-03-16?20:56??win_cilent\cilent.cpp.bak
?????文件??????53332??2011-03-17?15:00??win_cilent\Img.jpg
?????文件????????100??2011-03-17?20:29??win_cilent\sock_file_recv_send.h
?????文件???????4472??2011-03-17?21:27??win_cilent\win_cilent.dsp
?????文件????????873??2011-03-18?08:21??win_cilent\sock_file_recv_send.cpp
?????文件???????2758??2011-03-18?08:46??win_cilent\cilent.cpp
?????文件??????48640??2011-03-18?08:54??win_cilent\win_cilent.opt
?????文件????????528??2011-03-16?21:13??win_server\win_server.dsw
?????文件??????50176??2011-03-18?08:54??win_server\win_server.ncb
?????文件?????148480??2011-03-18?08:54??win_server\Debug\vc60.idb
?????文件??????77824??2011-03-18?08:54??win_server\Debug\vc60.pdb
?????文件?????180317??2011-03-18?08:52??win_server\Debug\win_server.exe
?????文件?????500736??2011-03-18?08:52??win_server\Debug\win_server.pdb
?????文件?????186980??2011-03-17?20:21??win_server\Debug\win_server.pch
?????文件??????12583??2011-03-17?20:29??win_server\Debug\sock_file_recv_send.obj
?????文件?????220636??2011-03-18?08:52??win_server\Debug\win_server.ilk
?????文件??????14934??2011-03-18?08:54??win_server\Debug\server.obj
?????文件????????975??2011-03-18?08:54??win_server\win_server.plg
?????文件?????????20??2011-03-16?21:14??win_server\WW_WW.txt
............此處省略14個文件信息
評論
共有 條評論