|
以下是我很无聊时乱写的Socket C 程序代码
如果你觉得你有空的话,帮我找一下错误啊
-------------------Client.c-------------------
' Create Socket -> Connect -> Read Msg
----------Start----------- '是一个客户端
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
int main(int argc,char *argv[])
{
int socketfd,rent;
struct socketaddr_in Severaddr;
char buffer[1024];
socketfd=socket(AF_INET,SOCK_STREAM,0);'建立Socket
bzero(&serveraddr,sizeof(serveraddr)); '清空结构变量
severaddr.sin_family=AF_INET;
severaddr.sin_port=htons(22);'端口号设为22
severaddr.sin_addr.s_addr=htonl(argv[1]);'把第一个命令行参数作为服务器地址传递
rent=connect(socketfd,(struct sockaddr *)(&serveraddr),sizeof(struct socketaddr);
rent=read(socketfd,buffer,sizeof(buffer));
buffer[rent]='';
printf("%s\n",buffer);
close(socketfd);
exit(1);
}
----------------End-------------------- |
|