当前位置: 技术问答>linux和unix
求助:各位大哥能给一个 socket 程序(服务器一个,客户端一个),谢谢!马上送分哦~
来源: 互联网 发布时间:2015-12-17
本文导语: 求助: 各位大哥能给一个 socket 程序(服务器一个,客户端一个) 马上送分哦~ 谢谢! | server: #include #include #include #include #include #include #define MAXLINE 1024 typedef struct sockaddr SA; int...
求助:
各位大哥能给一个 socket 程序(服务器一个,客户端一个)
马上送分哦~
谢谢!
各位大哥能给一个 socket 程序(服务器一个,客户端一个)
马上送分哦~
谢谢!
|
server:
#include
#include
#include
#include
#include
#include
#define MAXLINE 1024
typedef struct sockaddr SA;
int main(int argc, char **argv)
{
int listenfd, connfd;
socklen_t len;
struct sockaddr_in servaddr, cliaddr;
char buff[MAXLINE];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(1333); /* daytime server */
bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
listen(listenfd, 5);
for ( ; ; ) {
len = sizeof(cliaddr);
connfd = accept(listenfd, (SA *) &cliaddr, &len);
printf("connection from %s, port %dn",
inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
ntohs(cliaddr.sin_port));
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24srn", ctime(&ticks));
write(connfd, buff, strlen(buff));
close(connfd);
}
}
gcc server-test.c -o server-test
client:
#include
#include
#include
#include
#include
#define MAXLINE 1024
typedef struct sockaddr SA;
int main(int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
if (argc != 2)
{
printf("usage: a.out ");
exit (1);
}
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0))
#include
#include
#include
#include
#include
#include
#define MAXLINE 1024
typedef struct sockaddr SA;
int main(int argc, char **argv)
{
int listenfd, connfd;
socklen_t len;
struct sockaddr_in servaddr, cliaddr;
char buff[MAXLINE];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(1333); /* daytime server */
bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
listen(listenfd, 5);
for ( ; ; ) {
len = sizeof(cliaddr);
connfd = accept(listenfd, (SA *) &cliaddr, &len);
printf("connection from %s, port %dn",
inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
ntohs(cliaddr.sin_port));
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24srn", ctime(&ticks));
write(connfd, buff, strlen(buff));
close(connfd);
}
}
gcc server-test.c -o server-test
client:
#include
#include
#include
#include
#include
#define MAXLINE 1024
typedef struct sockaddr SA;
int main(int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
if (argc != 2)
{
printf("usage: a.out ");
exit (1);
}
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0))