Client端和Server端源代码

1,SErver端源码:
#include<sys/types.h>
#include<sys/socket.h>
#include<strings.h>
#include<string.h>
#include<arpa/inet.h>
#include<inttypes.h>
#include<unistd.h>
#include<errno.h>
#include<time.h>
#include<iostream>
#include<cstdlib>

using namespace std;

type struct MSG{
Header head;
Body body;
}

const int PORT = 1235;

main(int argc , char ** argv){
int port = 0 ;
if(argc > 1){
port = atoi(argv[1]);
}
if(port == 0) port = PORT;

int sockfd = socket(AF_INET ,SOCK_STREAM , 0);
int on = 1;
if(sockfd < 0){
cout << "cannot create socket: " << strerror(errno) << endl;
exit(1);
}

struct sockaddr_in me;
bzero(&me , sizeof(me));
me.sin_family = AF_INET;
me.sin_port = htons(port);
me.sin_addr.s_addr = htonl(INADDR_ANY);

if(bind(sockfd , (struct sockaddr *)&me , (socklen_t)sizeof(me)) != 0){
cout << "binding failed: " << strerror(errno) << endl;
close(sockfd);
exit(1);
}

socklen_t len = sizeof(me);
if(getsockname(sockfd , (struct sockaddr *)&me , &len) == 0){
cout << "local port: " << me.sin_port << endl;
char buf[256];
inet_ntop(AF_INET, &me.sin_addr , buf , 256);
cout << "local address: " << buf << endl;
}

if(listen(sockfd , 10) != 0){
cout << "listen failed: " << strerror(errno) << endl;
close(sockfd);
exit(1);
}

struct sockaddr_in client;
len = sizeof(client);

while(1){
int acceptedfd = accept(sockfd , (struct sockaddr *)&client , &len);
if(acceptedfd < 0){
cout << "accept failed: " << strerror(errno) << endl;
break;
}

char cliaddr[64];
inet_ntop(AF_INET , &client.sin_addr , cliaddr , 64);
cout << " client address: " << cliaddr << endl;
cout << " client port : " << ntohs(client.sin_port) << endl;

char test[50];
int ret;
if(ret = read(acceptedfd , test , sizeof(test)) < 0){
cout << "read error " << endl;
close(acceptedfd);
}
cout << "client string: " << test << endl;
cout << "success " << endl;
///


close(acceptedfd);

}
close(sockfd);
}

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <iostream>

using namespace std;

main(int argc , char **argv){
if(argc != 2){
cout << "Usage: " << argv[0] << "timeServer" << endl;
exit(1);
}
struct sockaddr_in server;
bzero(&server , sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(1666);
struct hostent *shost = gethostbyname(argv[1]);
if(!shost){
cout << "cannot get hostent" << endl;
exit(1);
}
memcpy(&server.sin_addr.s_addr,shost->h_addr_list[0],shost->h_length);
int sockfd =socket(AF_INET , SOCK_STREAM , 0);
if(socket < 0){
cout << "cannot create a socket: " << strerror(errno) << endl;
exit(1);
}
if(connect(sockfd , (struct sockaddr *)&server , sizeof(server))){
cout << "connect error: " << strerror(errno) << endl;
close(sockfd);
exit(1);
}
socklen_t len = sizeof(server);
if(getpeername(sockfd , (struct sockaddr *)&server, &len) == 0){
cout << "remote port: " << ntohs(server.sin_port) << endl;
char buf[64];
inet_ntop(AF_INET , &server.sin_addr , buf , 64);
cout << "remote address: " << buf << endl;
}
struct sockaddr_in me;
if(getsockname(sockfd , (struct sockaddr *)&me , &len) == 0){
cout << "local port: " << ntohs(me.sin_port) << endl;
char buf[64];
inet_ntop(AF_INET , &me.sin_addr , buf , 64);
cout << "remote address: " << buf << endl;
}
char *test = "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk";
if(write(sockfd , test , strlen(test)) < 0 ){
cout << "write error " << endl;
}
cout << "write success" << endl;


close(sockfd);
cout << "bbbbbbb" << endl;
exit(0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值