socket代码,socket详解

http://www.itjxue.com  2023-01-05 06:37  来源:未知  点击次数: 

socket 客户端可以向服务器端接收和发送文件,代码怎么写

之前写过这样的一个小程序,我发布在自己的博客上:

Server.c

#include?WinSock2.h

#include?WS2tcpip.h

#include?cstdio

#include?iostream

//?for?file?read

#include?fstream

#include?string

int?main()?{

/*?load?the?initializer?library?*/

WSADATA?wsaData;

WSAStartup(MAKEWORD(2,?2),?wsaData);

/*?list?the?each?fields?of?wsaData?*/

//?printf("wVersion:?%d\n",?wsaData.wVersion); //?514

//?printf("wHighVersion:?%d\n",?wsaData.wHighVersion); //?514

//?printf("iMaxSockets:?%d\n",?wsaData.iMaxSockets); //?0

//?printf("iMaxUdpDg:?%d\n",?wsaData.iMaxUdpDg); //?0

/*?you?may?not?print?the?lpVendorInfo?*/

//?printf("szDescription:?%s\n",?wsaData.szDescription); //?WinSock?2.0

//?printf("szSystemStatus:?%s\n",?wsaData.szSystemStatus); //?Running

/*?create?socket(address?family,?type,?protocol)?*/

int?socket_fd?=?socket(AF_INET,?SOCK_STREAM,?IPPROTO_TCP);

/*?initialize?the?addrinfo?hints?*/

struct?addrinfo?hints;

memset(hints,?0,?sizeof(hints));

hints.ai_family?=?AF_INET;

hints.ai_socktype?=?SOCK_STREAM;

hints.ai_protocol?=?IPPROTO_TCP;

hints.ai_flags?=?AI_PASSIVE;

struct?addrinfo?*result?=?nullptr;

/*?by?hints,?and?then?get?the?result(a?linked?list,?see?fields?below)?*/

getaddrinfo(NULL,?"8080",?hints,?result);

/*?the?member?fiekds?of?result?*/

//?printf("ai_family:?%d\n",?result-ai_family); //?2

//?printf("ai_socktype:?%d\n",?result-ai_socktype); //?1

//?printf("ai_protocol:?%d\n",?result-ai_protocol); //?6

//?printf("ai_flags:?%d\n",?result-ai_flags); //?0

//?printf("ai_canonname:?%s\n",?result-ai_canonname); //?(null)

//?printf("ai_a?ddrlen:?%d\n",?result-ai_addrlen); //?16

//?printf("ai_addr-sa_family:?%d\n",?result-ai_addr-sa_family); //?2

//?printf("ai_addr-sa_data:?%s\n",?result-ai_addr-sa_data); //?

//?printf("ai_next:?%x\n",?result-ai_next); //?0

/*?bind?the?socket?to?a?address?*/

/*?bind(socket,?sockaddr?name,?name?len)?*/

bind(socket_fd,?result-ai_addr,?result-ai_addrlen);

freeaddrinfo(result);

/*?listen?the?socket?*/

/*?listen(socket,?backlog)?*/

listen(socket_fd,?SOMAXCONN); /*?0x7fffffff?*/

while(true)?{

/*?accept?a?connection?*/

/*?accept(socket,?addr,?addrlen)?*/

int?client_fd?=?accept(socket_fd,?nullptr,?0);

printf("New?Connection?Eastablished...\n");

/*?Now?we?send?the?source?code?of?this?file?to?client?*/

std::ifstream?fin("c:\\source.cpp");

/*?store?it?into?a?buffer.?*/

while?(fin.good())?{

//?I?rememeber?there?is?a?method?called?getline

std::string?s;

getline(fin,?s);

//?we?need?a?new?line

s?=?s?+?'\n';

send(client_fd,?s.c_str(),?s.size(),?0);

}

fin.close();

/*?close?client?socket?*/

closesocket(client_fd);

}

/*?may?be?never?invoked?*/

closesocket(socket_fd);

WSACleanup();

return?0;

}

Client.c

#include?WinSock2.h

#include?cstdio

#include?iostream

#include?fstream

int?main()?{

/*?load?the?initializer?library?*/

WSADATA?wsaData;

WSAStartup(MAKEWORD(2,?2),?wsaData);

/*?create?socket?*/

int?socket_fd?=?socket(AF_INET,?SOCK_STREAM,?IPPROTO_TCP);

/*?connect?*/

sockaddr_in?addr;

addr.sin_family?=?AF_INET;

addr.sin_addr.s_addr?=?inet_addr("127.0.0.1");

addr.sin_port?=?htons(8080);

/*?connect(socket,?sockaddr,?namelen)?*/

connect(socket_fd,?(sockaddr*)addr,?sizeof(addr));

/*?buffer?memory?*/

char?buf[1024];

/*?open?a?file?for?write?*/

std::ofstream?fout("source.cpp",?std::ios_base::trunc);

while?(true)?{

int?read?=?recv(socket_fd,?buf,?1024,?0);?

if?(read?==?0)?{

fout.close();

printf("Connection?Closed...\n");

break;

}

if?(read??1024)?{

printf("Buffer?Overflow...\n");

}?else?{

buf[read?-?1]?=?0;

/*?now?we?can?print?the?bytes?from?server?*/

fout??buf??std::endl;

}

}

/*?close?socket?*/

closesocket(socket_fd);

WSACleanup();

return?0;

}

linux socket程序 求代码运行结果

ret?=?write(socket_fd,?send_buff,?strlen(send_buff));

应该改为:

ret?=?write(new_socket_fd,?send_buff,?strlen(send_buff));

accept成功后,所有的操作,都要针对新获得的句柄来操作,不然会引发错误,造成主程序异常退出。

socket?这段代码是什么意思?

myscoket=new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp) //实例化一个Socket对象。

mysocket.beging.connect(myserver,new asycallback(connectback),mysocket;connectreset.waitone();)

//象系统投递一个异步连接远程服务器的操作。连接成功后,系统会自动调用回调函数connectback.在connectback函数内实现连接成功后的操作就好了。

socket c 这代码是什么意思?

这段代码是使用 windows socket 函数库编写的一个客户端,但是只是去连接了服务器 192.168.1.1:3578

如果连接成功,connect 函数的返回值为 0, 则后续可以调用 recv 和 send 实现数据的接收和发送。

(责任编辑:IT教学网)

更多