Socket编程

Socket编程,服务器和客服端的实现

//服务端server.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 6000 /*服务器监听端口号 */
#define BACKLOG 10 /* 最大同时连接请求数 */
#define MAXDATASIZE 100

main()
{
 char buf[MAXDATASIZE];
 int sockfd,client_fd; /*sock_fd:监听socket;client_fd:数据传输socket */
 struct sockaddr_in my_addr; /* 本机地址信息 */
 struct sockaddr_in remote_addr; /* 客户端地址信息 */

 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
 {
  perror("socket创建出错!");
  exit(1);
 }
 my_addr.sin_family=AF_INET;
 my_addr.sin_port=htons(SERVPORT);
 my_addr.sin_addr.s_addr = INADDR_ANY;
 bzero(&(my_addr.sin_zero),8);
 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
 {
  perror("bind出错!");
  exit(1);
 }
 if (listen(sockfd, BACKLOG) == -1)
 {
  perror("listen出错!");
  exit(1);
 }
 while(1)
 {
  sin_size = sizeof(struct sockaddr_in);
  if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1)
  {
   perror("accept出错");
   continue;
  }
  printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));
  if (!fork())
  { /* 子进程代码段 */
   if ((recvbytes=recv(client_fd, buf, MAXDATASIZE, 0)) ==-1)
   {
    perror("recv出错!");
    close(client_fd);
    exit(0);
   }
   buf[recvbytes] = '\0';
   printf("from client Received: %s",buf);

   if (send(client_fd, "thanks!\n", 8, 0) == -1)
    perror("send出错!");
   close(client_fd);
   exit(0);
  }
  close(client_fd);
 }
}

//客户端client.c
#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 6000
#define MAXDATASIZE 100

main(int argc, char *argv[])
{
 int sockfd, recvbytes;
 char buf[MAXDATASIZE];
 struct hostent *host;
 struct sockaddr_in serv_addr;
 if (argc < 2)
 {
  fprintf(stderr,"Please enter the server's hostname!\n");
  exit(1);
 }
 if((host=gethostbyname(argv[1]))==NULL)
 {
  herror("gethostbyname出错!");
  exit(1);
 }
 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
 {
  perror("socket创建出错!");
  exit(1);
 }
 serv_addr.sin_family=AF_INET;
 serv_addr.sin_port=htons(SERVPORT);
 serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
 bzero(&(serv_addr.sin_zero),8);
 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1)
 {
  perror("connect出错!");
  exit(1);
 }

if (send(sockfd, "hello!\n", 7, 0) == -1)
 {
  perror("send出错!");
  exit(1);
 }
 
 if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1)
 {
  perror("recv出错!");
  exit(1);
 }
 buf[recvbytes] = '\0';
 printf("Received: %s",buf);
 close(sockfd);
}

UDP

//<udp_client.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(void)
{
	int sfd; char buf[1024]; int n, i;
	struct sockaddr_in serv_addr, cli_addr;
	socklen_t len;

	sfd = socket(AF_INET, SOCK_DGRAM, 0);

	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(8000);
	inet_pton(AF_INET, "192.168.0.36", &serv_addr.sin_addr.s_addr);

	while(1) {
		if(fgets(buf, 1024, stdin) == NULL)
			break;
		sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
		n = recvfrom(sfd, buf, 1024, 0, NULL, NULL);
		write(STDOUT_FILENO, buf, n);
	}
	close(sfd);

	return 0;
}

//<udp_server.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(void)
{
	int sfd; char buf[1024]; int n, i;
	struct sockaddr_in serv_addr, cli_addr;
	socklen_t len;

	sfd = socket(AF_INET, SOCK_DGRAM, 0);

	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(8000);
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

	bind(sfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));

	while(1) {
		len = sizeof(cli_addr);
		n = recvfrom(sfd, buf, 1024, 0, (struct sockaddr *)&cli_addr, &len);
		for(i = 0; i < n; i++)
			buf[i] = toupper(buf[i]);
		sendto(sfd, buf, n, 0, (struct sockaddr *)&cli_addr, len);
	}
		close(sfd);

		return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值