use ggg
This commit is contained in:
parent
3b48749414
commit
c0eea479aa
74
cc.c
Normal file
74
cc.c
Normal file
@ -0,0 +1,74 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sockfd = 0, n = 0;
|
||||
char recvBuff[1024];
|
||||
struct sockaddr_in serv_addr;
|
||||
|
||||
if(argc != 2)
|
||||
{
|
||||
printf("\n Usage: %s <ip of server> \n",argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(recvBuff, '0',sizeof(recvBuff));
|
||||
|
||||
/* a socket is created through call to socket() function */
|
||||
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||
{
|
||||
printf("\n Error : Could not create socket \n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&serv_addr, '0', sizeof(serv_addr));
|
||||
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(5000);
|
||||
|
||||
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
|
||||
{
|
||||
printf("\n inet_pton error occured\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Information like IP address of the remote host and its port is
|
||||
* bundled up in a structure and a call to function connect() is made
|
||||
* which tries to connect this socket with the socket (IP address and port)
|
||||
* of the remote host
|
||||
*/
|
||||
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
|
||||
{
|
||||
printf("\n Error : Connect Failed \n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Once the sockets are connected, the server sends the data (date+time)
|
||||
* on clients socket through clients socket descriptor and client can read it
|
||||
* through normal read call on the its socket descriptor.
|
||||
*/
|
||||
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
|
||||
{
|
||||
recvBuff[n] = 0;
|
||||
if(fputs(recvBuff, stdout) == EOF)
|
||||
{
|
||||
printf("\n Error : Fputs error\n");
|
||||
}
|
||||
}
|
||||
|
||||
if(n < 0)
|
||||
{
|
||||
printf("\n Read error \n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
73
ccc.c
Normal file
73
ccc.c
Normal file
@ -0,0 +1,73 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netdb.h>
|
||||
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
// insert code here...
|
||||
printf("Hello, World!\n");
|
||||
|
||||
int client_sockfd;
|
||||
int len;
|
||||
struct sockaddr_in remota_addr; //服务器端网络地址结构体
|
||||
char buf[BUFSIZ]; //数据传送的缓冲区
|
||||
memset(&remota_addr,0,sizeof(remota_addr)); //数据初始化--清零
|
||||
remota_addr.sin_family = AF_INET; //设置为IPV4通信
|
||||
remota_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
remota_addr.sin_port = htons(8000); //服务器端口号
|
||||
|
||||
//创建客户端套接字--Ipv4协议,面向连接通信,TCP协议
|
||||
//成功,返回0 ,失败返回-1
|
||||
if ((client_sockfd=socket(PF_INET, SOCK_STREAM, 0))<0) {
|
||||
perror("socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
//将套接字绑定到服务器的网络地址上
|
||||
if (connect(client_sockfd, (struct sockaddr *)&remota_addr, sizeof(struct sockaddr))<0) {
|
||||
perror("connect");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("connect to server\n");
|
||||
|
||||
len = recv(client_sockfd, buf, BUFSIZ, 0); //接受服务器端消息
|
||||
buf[len]='/0';
|
||||
printf("%s",buf); //打印服务器端消息
|
||||
|
||||
//循环的发送信息并打印接受消息--recv返回接收到的字节数,send返回发送的字节数
|
||||
while (1) {
|
||||
memset(buf,0,sizeof(buf));
|
||||
|
||||
printf("Enter string to send");
|
||||
scanf("%s",buf);
|
||||
// if (!strcmp(buf,"quit")) {
|
||||
// break;
|
||||
// }
|
||||
|
||||
len=send(client_sockfd,buf,strlen(buf),0);
|
||||
memset(buf,0,sizeof(buf));
|
||||
|
||||
len=recv(client_sockfd,buf,BUFSIZ,0);
|
||||
buf[len]='\0';
|
||||
printf("received:%s\n",buf);
|
||||
if(buf[0]=='q'){
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
close(client_sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
63
ss.c
Normal file
63
ss.c
Normal file
@ -0,0 +1,63 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int listenfd = 0, connfd = 0;
|
||||
struct sockaddr_in serv_addr;
|
||||
|
||||
char sendBuff[1025];
|
||||
time_t ticks;
|
||||
|
||||
/* creates an UN-named socket inside the kernel and returns
|
||||
* an integer known as socket descriptor
|
||||
* This function takes domain/family as its first argument.
|
||||
* For Internet family of IPv4 addresses we use AF_INET
|
||||
*/
|
||||
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
memset(&serv_addr, '0', sizeof(serv_addr));
|
||||
memset(sendBuff, '0', sizeof(sendBuff));
|
||||
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
serv_addr.sin_port = htons(5000);
|
||||
|
||||
/* The call to the function "bind()" assigns the details specified
|
||||
* in the structure 『serv_addr' to the socket created in the step above
|
||||
*/
|
||||
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
|
||||
|
||||
/* The call to the function "listen()" with second argument as 10 specifies
|
||||
* maximum number of client connections that server will queue for this listening
|
||||
* socket.
|
||||
*/
|
||||
listen(listenfd, 10);
|
||||
|
||||
while(1)
|
||||
{
|
||||
/* In the call to accept(), the server is put to sleep and when for an incoming
|
||||
* client request, the three way TCP handshake* is complete, the function accept()
|
||||
* wakes up and returns the socket descriptor representing the client socket.
|
||||
*/
|
||||
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
|
||||
|
||||
/* As soon as server gets a request from client, it prepares the date and time and
|
||||
* writes on the client socket through the descriptor returned by accept()
|
||||
*/
|
||||
ticks = time(NULL);
|
||||
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
|
||||
write(connfd, sendBuff, strlen(sendBuff));
|
||||
|
||||
close(connfd);
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
73
sss.c
Normal file
73
sss.c
Normal file
@ -0,0 +1,73 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
// insert code here...
|
||||
printf("Hello, World!\n");
|
||||
|
||||
int server_sockfd; //服务端套接字
|
||||
int client_sockfd; //客户端套接字
|
||||
int len;
|
||||
struct sockaddr_in my_add; //服务器网络地址结构体
|
||||
struct sockaddr_in remote_addr; //客户端网络地址结构体
|
||||
socklen_t sin_size;
|
||||
char buf[BUFSIZ]; //数据传送的缓冲区
|
||||
memset(&my_add,0,sizeof(my_add)); //数据初始化--清零
|
||||
my_add.sin_family = AF_INET; //设置为IP通信
|
||||
my_add.sin_addr.s_addr = INADDR_ANY; //服务器IP地址--允许连接到所有本地地址上
|
||||
my_add.sin_port = htons(8000); //服务器端口号
|
||||
|
||||
//创建服务器端套接字--IPV4协议 ,面向连接通信,TCP协议
|
||||
if((server_sockfd=socket(PF_INET, SOCK_STREAM, 0))<0) {
|
||||
perror("socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
//将套接字绑定到服务器的网络地址上
|
||||
if (bind(server_sockfd, (struct sockaddr *)&my_add, sizeof(struct sockaddr))<0) {
|
||||
perror("bind");
|
||||
return 1;
|
||||
}
|
||||
|
||||
//监听连接请求--监听队列长度为5
|
||||
listen(server_sockfd, 5);
|
||||
sin_size = sizeof(struct sockaddr_in);
|
||||
|
||||
//等待客户端连接请求到达
|
||||
if ((client_sockfd=accept(server_sockfd, (struct sockaddr *)&remote_addr, &sin_size))<0) {
|
||||
perror("accept");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr));
|
||||
len = send(client_sockfd, "welcome to my server\n", 21, 0); //发送欢迎信息
|
||||
|
||||
//接受客户端的数据并将其发送给客户端--recv返回接收到的字节数,send返回发送的字节数
|
||||
while (1) {
|
||||
memset(buf,0,sizeof(buf));
|
||||
len = recv(client_sockfd, buf, BUFSIZ, 0);
|
||||
send(client_sockfd,buf, len, 0);
|
||||
buf[len]='\0';
|
||||
printf("receive:%s\n",buf);
|
||||
if(buf[0]=='q'){
|
||||
send(client_sockfd,'q', 1, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close(client_sockfd);
|
||||
close(server_sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user