Technorati 标签: socket
算是socket的一个小练习,免得自己眼高手低。
聊天一方充当server,而另一方充当client。
在accept使用上卡了很久,一直提示Bad Address。结果发现最后一个参数有的是int,有的是int*,目前还不理解是为什么。
直接贴下程序,目前只能等对方说一句话后才能说,不知道怎么才能改进一下呢?
想到的就是想qq聊天窗口那样,输入与显示要分开才行。
如果实在terminal,如果输入的时候对方的消息过来,应该怎么办?
一方面要随时可以输入(scanf等待),一方面要随时接收输出,应该时要使用多线程没错的。不过还是没想到怎么具体解决.
贴下现有的代码
充当客户端的一方:
/*
* =====================================================================================
* Filename: TalkServer.c
* Description:
*
* Version: 1.0
* Created: 08/04/2012 02:35:01 PM
*
* Author: zhy (), [email protected]
* =====================================================================================
*/
#include <stdio.h>
#include <error.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define TALKPORT 1234
int main()
{
int sockfd, clientSockfd, recvbytes;
struct sockaddr_in myAddr;
struct sockaddr_in clientAddr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket create error\n");
exit(1);
}
myAddr.sin_family = AF_INET;
myAddr.sin_port = htons(TALKPORT);
myAddr.sin_addr.s_addr = INADDR_ANY;
bzero(&myAddr.sin_zero, 8);
if (bind(sockfd, (struct sockaddr*)&myAddr, sizeof(struct sockaddr)) == -1)
{
perror("socket bind error\n");
exit(1);
}
if (listen(sockfd, 10) == -1)
{
printf("socket listen error\n");
exit(1);
}
while (1)
{
unsigned int sin_size = sizeof(struct sockaddr_in);
if ((clientSockfd = accept(sockfd, (struct sockaddr*)&clientAddr, &sin_size)) == -1)
{
perror("socket accept error");
exit(1);
}
else
{
send(clientSockfd, "u got me!", 10, 0);
break;
}
}
while (1)
{
char recvmsg[256] = {0};
if ((recvbytes = recv(clientSockfd, recvmsg, 256, 0)) == -1)
{
printf("recv msg: %s error\n", recvmsg);
exit(1);
}
else
{
recvmsg[recvbytes] = '\0';
if (strcmp(recvmsg, "greedisgood") == 0)
{
send(clientSockfd, "greedisgood", 12, 0);
break;
}
printf("says: %s\n", recvmsg);
char sendmsg[256] = {0};
printf(">: ");
gets(sendmsg);
if (send(clientSockfd, sendmsg, sizeof(sendmsg), 0) == -1)
{
printf("send msg: %s error\n", sendmsg);
exit(1);
}
}
}
close(clientSockfd);
close(sockfd);
return 0;
}
充当客户端的一方:
/* * ===================================================================================== * Filename: TalkClient.c * Description: talker as client * * Version: 1.0 * Created: 08/04/2012 03:05:52 PM * * Author: zhy (), [email protected] * ===================================================================================== */ #include <string.h> #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define TALKPORT 1234 int main(int argc, char* argv[]) { int serverSockfd, recvbytes; struct sockaddr_in serverSockAddr; struct hostent* host; if ((serverSockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("socket create error\n"); exit(1); } if ((host = gethostbyname(argv[1])) == NULL) { printf("gethostbyname error\n"); exit(1); } serverSockAddr.sin_family = AF_INET; serverSockAddr.sin_port = htons(TALKPORT); serverSockAddr.sin_addr = *((struct in_addr*)host->h_addr); bzero(&serverSockAddr.sin_zero, 8); printf("server: %s\n", inet_ntoa(serverSockAddr.sin_addr)); if (connect(serverSockfd, (struct sockaddr*)(&serverSockAddr), sizeof(struct sockaddr)) == -1) { printf("socket connect error\n"); exit(1); } while (1) { char recvmsg[256] = {0}; if ((recvbytes = recv(serverSockfd, recvmsg, 256, 0)) == -1) { printf("recv msg: %s error\n", recvmsg); exit(1); } else { recvmsg[recvbytes] = '\0'; if (strcmp(recvmsg, "greedisgood") == 0) { send(serverSockfd, "greedisgood", 12, 0); break; } printf("says: %s\n", recvmsg); char sendmsg[256] = {0}; printf(">: "); gets(sendmsg); if (send(serverSockfd, sendmsg, sizeof(sendmsg), 0) == -1) { printf("send msg: %s error\n", sendmsg); exit(1); } } } close(serverSockfd); return 0; }其中says开始即为对方说的话,而>:则为自己收入的内容
实例:
server端: client端: