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);
}
#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);
}