关闭

(2)网络编程之绑定socket

标签: 编程网络structserversocketinternet
375人阅读 评论(0) 收藏 举报
分类:

Structure

/* Internet address. */
struct in_addr {
	__u32	s_addr;
};
/* Structure describing an Internet (IP) socket address. */
#define __SOCK_SIZE__	16		/* sizeof(struct sockaddr)	*/
struct sockaddr_in {
  sa_family_t		sin_family;	/* Address family		*/
  unsigned short int	sin_port;	/* Port number			*/
  struct in_addr	sin_addr;	/* Internet address		*/

  /* Pad to size of `struct sockaddr'. */
  unsigned char		__pad[__SOCK_SIZE__ - sizeof(short int) -
			sizeof(unsigned short int) - sizeof(struct in_addr)];
};

Src

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define RECV_BUF_SIZE 	1024
#define CMD_PARAMS		5

int main(int argc, char *argv[])
{
	int sockfd = 0;
	int ret = 0;
	char recv_buf[RECV_BUF_SIZE];
	struct sockaddr_in server_addr, client_addr;

	memset(recv_buf, 0, sizeof(recv_buf));
	memset(&server_addr, 0, sizeof(struct sockaddr_in));
	memset(&client_addr, 0, sizeof(struct sockaddr_in));

	if (argc != CMD_PARAMS)
	{
		fprintf(stderr, "Usage: %s server port client port\n", argv[0]);
		return -1;
	}

	sockfd = socket(AF_INET, SOCK_STREAM, 0);  
	if (sockfd < 0)  
	{  
		fprintf(stderr, "Create socket fail\n");  
		return -1;  
	}

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(atoi(argv[2]));
	ret = inet_aton(argv[1], (struct in_addr *)(&server_addr.sin_addr.s_addr));
	if (0 == ret)
	{
		fprintf(stderr, "Server address %s error\n", argv[1]);
		return -1;
	}
	
	client_addr.sin_family = AF_INET;
	client_addr.sin_port = htons(atoi(argv[4]));
	ret = inet_aton(argv[3], (struct in_addr *)(&client_addr.sin_addr.s_addr));
	if (0 == ret)
	{
		fprintf(stderr, "Client address %s error\n", argv[1]);
		return -1;
	}
	
	ret = bind(sockfd, (struct sockaddr *)(&client_addr), sizeof(struct sockaddr));
	if (-1 == ret)
	{
		fprintf(stderr, "Bind client socket and address fail, %s\n", argv[3]);
		return -1;
	}
	
	ret = connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(server_addr));
	if (ret != 0)
	{
		fprintf(stderr, "Connect server %s:%d fail\n", argv[1], atoi(argv[2]));
		return -1; 
	}
	
	recv(sockfd, recv_buf, RECV_BUF_SIZE, 0);
	printf("%s", recv_buf);
	close(sockfd);

	return 0;
}

Makefile

target := bind_exp
obj = $(patsubst *.c, *.o, $(wildcard *.c))
$(target): $(obj)
        gcc -Wall -o $(target) $(obj) 
clean:
        rm -r $(target) *.o

Do

root@ubuntuServer:~/test/networkprogram# make
gcc -Wall -o bind_exp  bindip.c 
root@ubuntuServer:~/test/networkprogram# ./bind_exp 192.168.1.109 22 192.168.1.109 6000
SSH-2.0-OpenSSH_5.3p1 Debian-3ubuntu7
root@ubuntuServer:~/test/networkprogram# netstat -an|grep 6000
tcp        0      0 192.168.1.109:6000      192.168.1.109:22        TIME_WAIT 




 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8617次
    • 积分:179
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档