当前位置: 技术问答>linux和unix
请教各位高手如何在Linux下实现局域网内的通信
来源: 互联网 发布时间:2014-12-30
本文导语: 请教各位高手如何在Linux下实现局域网内的通信 小弟刚工作,请大家多帮帮忙, 能否介绍一下相关内容,推荐文章,书籍等 有源码(c/c++)更好 | 《用tcp/ip进行网际互连》第三卷 里面有源代码 | ...
请教各位高手如何在Linux下实现局域网内的通信
小弟刚工作,请大家多帮帮忙,
能否介绍一下相关内容,推荐文章,书籍等
有源码(c/c++)更好
小弟刚工作,请大家多帮帮忙,
能否介绍一下相关内容,推荐文章,书籍等
有源码(c/c++)更好
|
《用tcp/ip进行网际互连》第三卷
里面有源代码
里面有源代码
|
tcpip例子:
//client
#include
#include
#include
#include
#include
#include
#include
#define MAX_BUF_SIZE (300)
static char buf[MAX_BUF_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int main()
{
int sock;
struct sockaddr_in sin;
int size;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
perror("client socket");
return -1;
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr("192.168.21.207");
sin.sin_port = htons(1685);
if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("client connect");
return -1;
}
size = 0;
if (sock != -1)
{
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
}
printf("client send %d bytesn", size);
close(sock);
}
//server
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_BUF_SIZE (1024)
static char buf[MAX_BUF_SIZE];
int display_address(const char *msg, struct sockaddr_in* p_sin)
{
if(p_sin->sin_family != AF_INET)
{
fprintf(stderr, "Not an Internet socket.n");
return -1;
}
fprintf(stdout, "%s ==> address is: %s : %dn", msg, inet_ntoa(p_sin->sin_addr), ntohs(p_sin->sin_port));
return 0;
}
int main()
{
int sock, serv;
struct sockaddr_in sin;
int size;
fd_set set;
FD_ZERO(&set);
serv = socket(AF_INET, SOCK_STREAM, 0);
if (serv == -1)
{
perror("server socket");
return -1;
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(1685);
if (bind(serv, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("server bind");
return -1;
}
listen(serv, 5);
memset(&sin, 0, sizeof(sin));
size = sizeof(sin);
if (getsockname(serv, (struct sockaddr *)&sin, &size) == -1)
perror("serv sockname");
display_address("server socket name", &sin);
sock = accept(serv, NULL, NULL);
if (sock != -1)
{
memset(&sin, 0, sizeof(sin));
size = sizeof(sin);
if (getpeername(sock, (struct sockaddr *)&sin, &size) == -1)
perror("sock peername");
display_address("sock peer name", &sin);
memset(&sin, 0, sizeof(sin));
size = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &size))
perror("sock sockname");
display_address("sock socket name", &sin);
//sleep(1);
while(1)
{
FD_SET(sock, &set);
select(FD_SETSIZE, &set, NULL, NULL, NULL);
size = recv(sock, buf, MAX_BUF_SIZE, 0);
if (size
//client
#include
#include
#include
#include
#include
#include
#include
#define MAX_BUF_SIZE (300)
static char buf[MAX_BUF_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int main()
{
int sock;
struct sockaddr_in sin;
int size;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
perror("client socket");
return -1;
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr("192.168.21.207");
sin.sin_port = htons(1685);
if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("client connect");
return -1;
}
size = 0;
if (sock != -1)
{
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
}
printf("client send %d bytesn", size);
close(sock);
}
//server
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_BUF_SIZE (1024)
static char buf[MAX_BUF_SIZE];
int display_address(const char *msg, struct sockaddr_in* p_sin)
{
if(p_sin->sin_family != AF_INET)
{
fprintf(stderr, "Not an Internet socket.n");
return -1;
}
fprintf(stdout, "%s ==> address is: %s : %dn", msg, inet_ntoa(p_sin->sin_addr), ntohs(p_sin->sin_port));
return 0;
}
int main()
{
int sock, serv;
struct sockaddr_in sin;
int size;
fd_set set;
FD_ZERO(&set);
serv = socket(AF_INET, SOCK_STREAM, 0);
if (serv == -1)
{
perror("server socket");
return -1;
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(1685);
if (bind(serv, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("server bind");
return -1;
}
listen(serv, 5);
memset(&sin, 0, sizeof(sin));
size = sizeof(sin);
if (getsockname(serv, (struct sockaddr *)&sin, &size) == -1)
perror("serv sockname");
display_address("server socket name", &sin);
sock = accept(serv, NULL, NULL);
if (sock != -1)
{
memset(&sin, 0, sizeof(sin));
size = sizeof(sin);
if (getpeername(sock, (struct sockaddr *)&sin, &size) == -1)
perror("sock peername");
display_address("sock peer name", &sin);
memset(&sin, 0, sizeof(sin));
size = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &size))
perror("sock sockname");
display_address("sock socket name", &sin);
//sleep(1);
while(1)
{
FD_SET(sock, &set);
select(FD_SETSIZE, &set, NULL, NULL, NULL);
size = recv(sock, buf, MAX_BUF_SIZE, 0);
if (size