Linux??C????socket?????????
???????????? ???????[ 2014/7/2 14:29:26 ] ??????????????? ??????
/*************************************
??????? client.c
linux ??socket????????? - ????????
???????????? 0x8888 ??????????????????????????????ò???????
???????????? 192.168.1.104
????:kikilizhm#163.com (??#???@)
*/
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <string.h>
int main()
{
int cfd; /* ????????? */
int recbytes;
int sin_size;
char buffer[1024]={0}; /* ????????? */
struct sockaddr_in s_add??c_add; /* ?洢??????????ip????????????? */
unsigned short portnum=0x8888; /* ????????????????????????????????? */
printf("Hello??welcome to client !
");
/* ????socket ???????????TCP?????? */
cfd = socket(AF_INET?? SOCK_STREAM?? 0);
if(-1 == cfd)
{
printf("socket fail !
");
return -1;
}
printf("socket ok !
");
/* ????????????ip???????????????????????? */
bzero(&s_add??sizeof(struct sockaddr_in));
s_add.sin_family=AF_INET;
s_add.sin_addr.s_addr= inet_addr("192.168.1.104"); /* ip????4??????Σ????????????????ip???и??? */
s_add.sin_port=htons(portnum); /* ????htons???short???????????????????????????????????
??2?????????????????????????????ntohsЧ?????????????????????????htonl??ntohl??
??????4??????Ρ???0x12345678???0x78563412????????????????????????????????????????
PPC??cpu?????x86??cpu?С???arm??????????С????????????????????????
*/
printf("s_addr = %#x ??port : %#x
"??s_add.sin_addr.s_addr??s_add.sin_port); /* ????????????С??
????????????????????? */
/* ?????????????????????????socket??????????????????????????С */
if(-1 == connect(cfd??(struct sockaddr *)(&s_add)?? sizeof(struct sockaddr)))
{
printf("connect fail !
");
return -1;
}
printf("connect ok !
");
/*????????????????????*/
if(-1 == (recbytes = read(cfd??buffer??1024)))
{
printf("read data fail !
");
return -1;
}
printf("read ok
REC:
");
buffer[recbytes]='
|