当前位置: 技术问答>linux和unix
socket客户端和服务器端的问题
来源: 互联网 发布时间:2016-05-28
本文导语: //客户端 #include #include #include #include #include #include #define MAX_BUF_SIZE 1024 void udpc_requ(int sockfd, const struct sockaddr_in *adr, int len) { char buffer[MAX_BUF_SIZE]; int n; while(1) { /*read form keyboard*/ fgets(buf...
//客户端
#include
#include
#include
#include
#include
#include
#define MAX_BUF_SIZE 1024
void udpc_requ(int sockfd, const struct sockaddr_in *adr, int len)
{
char buffer[MAX_BUF_SIZE];
int n;
while(1)
{
/*read form keyboard*/
fgets(buffer, MAX_BUF_SIZE, stdin);
sendto(sockfd, buffer, strlen(buffer), 0, addr, len);
bzero(buffer, MAX_BUF_SIZE);
/*read from internet,write onto screen*/
n = recvfrom(sockfd, buffer, MAX_BUF_SIZE, 0, NULL, NULL);
buffer[n] = 0;
fputs(buffer, stdout);
}
}
int main(int argc, char **argv)
{
int sockfd, port;
struct sockaddr_in addr;
if(argc != 3)
{
fprintf(stderr, "usage:%s server_ip server_portn", argv[0]);
exit(1);
}
if((port = atoi(argv[2]))
#include
#include
#include
#include
#include
#include
#define MAX_BUF_SIZE 1024
void udpc_requ(int sockfd, const struct sockaddr_in *adr, int len)
{
char buffer[MAX_BUF_SIZE];
int n;
while(1)
{
/*read form keyboard*/
fgets(buffer, MAX_BUF_SIZE, stdin);
sendto(sockfd, buffer, strlen(buffer), 0, addr, len);
bzero(buffer, MAX_BUF_SIZE);
/*read from internet,write onto screen*/
n = recvfrom(sockfd, buffer, MAX_BUF_SIZE, 0, NULL, NULL);
buffer[n] = 0;
fputs(buffer, stdout);
}
}
int main(int argc, char **argv)
{
int sockfd, port;
struct sockaddr_in addr;
if(argc != 3)
{
fprintf(stderr, "usage:%s server_ip server_portn", argv[0]);
exit(1);
}
if((port = atoi(argv[2]))