- 论坛徽章:
- 0
|
最近在看些应用方面编程,好多都不是很熟练,好想有关于这些的项目做呀.最近会把TCP/UDP这些Linux的socket编程部分.最终为vxworks上面的编程作准备
/*int connect (int sockfd,struct sockaddr * sever_addr,int addrlen)
ps:150.164.100.68 is the ip of www.cctv.com */
/* error information
error sentence:
if(s=(socket(AF_INET,SOCK_STREAM,0))
#include stdio.h>
#include unistd.h>
#include sys/types.h>
#include sys/socket.h>
#include netinet/in.h>
#include arpa/inet.h>
#define PORT 80
#define REMOTE_IP "150.164.100.68"
int main(int argc,char *argv[])
{
int s;
struct sockaddr_in addr;
int addr_len = sizeof(struct sockaddr_in);
char mybuffer[256];
/*socket one*/
/*pay attention the if sentence */
if((s=socket(AF_INET,SOCK_STREAM,0))0)
{
perror("error");
exit(1);
}
else
{
printf("socket created.\n");
printf("socket id : %d\n",s);
}
/*socket parameter init*/
bzero(&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr(REMOTE_IP);
/*connect */
if(connect(s,&addr,sizeof(addr))0)
{
perror("connect");
exit(1);
}
else
{
printf("connected ok!\n");
printf("remote ip : %s\n",REMOTE_IP);
printf("remote port : %d\n",PORT);
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/102267/showart_2144012.html |
|