/*
* chat_client.h
*
* Created on: 2012-4-19
* Author: root
*/
#ifndef CHAT_CLIENT_H_
#define CHAT_CLIENT_H_
#include <iostream>
#include <string>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
using namespace std;
#include <pthread.h>
#include <vector>
#define IP "127.0.0.1"
#define PORT 6000
typedef struct client_pock
{
char LogName[20];//保存自己的名字
char group[4];//保存是否要群发
char Acceptname[20];//保存要发的对方
char msg[200];//保存要发给对方的消息
} pocket;
class chat_client
{
static int clientfd;
public:
chat_client(){};
~chat_client(){};
static void* handle_msg(void* temp);
int NetInit();
void chat_info();
};
#endif /* CHAT_CLIENT_H_ */
/*
* chat_client.cpp
*
* Created on: 2012-4-19
* Author: root
*/
#include "chat_client.h"
int chat_client::clientfd ;
void* chat_client::handle_msg(void* temp)
{
while(1)
{
pocket pt;
char buf[1024];
memset(buf,0,1024);
int n = read(clientfd,buf,1024);
if(n <= 0)
{
continue;
}
char* pbuf = buf;
memcpy(&pt.LogName,pbuf,20);
cout << "你好" << pt.LogName <<"你有来自";
pbuf += 20;
memcpy(&pt.group,pbuf,4);
if(!strcmp(pt.group,"y"))
{
cout << "安卓宜的群发消息,发消息的人是";
pbuf += 4;
memcpy(&pt.Acceptname,pbuf,20);
cout << pt.Acceptname << "消息是: ";
pbuf += 20;
memcpy(&pt.msg,pbuf,200);
cout << pt.msg << endl;
}else
{
pbuf += 4;
memcpy(&pt.Acceptname,pbuf,20);
cout << pt.Acceptname << "的消息: " ;
pbuf += 20;
memcpy(&pt.msg,pbuf,200);
cout << pt.msg << endl;
}
}
}
int chat_client::NetInit()
{
clientfd = socket(PF_INET,SOCK_STREAM,0);
if(clientfd < 0)
{
return -1;
}
struct sockaddr_in sock_cc;
sock_cc.sin_family = PF_INET;
sock_cc.sin_port = htons(PORT);
inet_pton(PF_INET,IP,&sock_cc.sin_addr.s_addr);
if( connect(clientfd,(struct sockaddr*)&sock_cc,sizeof(sock_cc) ) < 0 )
{
throw "connect failed";
}
return 0;
}
void chat_client::chat_info()
{
pthread_t client_pid = -1;
pthread_create(&client_pid,0,handle_msg,0);
pocket send_pock;
char logname[20];
char groupid[4];
memset(groupid,0,4);
memset(logname,0,20);
cout << "登录后,在要发送消息就只需发送(是否要群发消息/收信人姓名/信内容)"<< endl;
cout << "请输入您的用户名: " << endl;
cin.getline(logname,20);
memcpy(&send_pock.LogName,logname,20);
while(1)
{
cout << "是否要群发消息(yes/no): " << endl ;
cin.getline(groupid,4);
memcpy(&send_pock.group, groupid,sizeof(groupid));
char name[20];
char msg[200];
memset(name,0,20);
memset(msg,0,200);
if(!strcmp(groupid,"y"))
{
memcpy(&send_pock.Acceptname,logname,20);
cout << "请您输入发送的消息: " << endl;
cin.getline(msg,200);
memcpy(&send_pock.msg,msg,200);
char buf[1024];
memset(buf,0,1024);
memcpy(buf,&send_pock,sizeof(send_pock));
}else
{
cout << "请您输入接受方的名字: " << endl;
cin.getline(name,20);
memcpy(&send_pock.Acceptname,name,20);
cout << "请您输入发送的消息: " << endl;
cin.getline(msg,200);
memcpy(&send_pock.msg,msg,200);
}
char buf[1024];
memset(buf,0,1024);
memcpy(buf,&send_pock,sizeof(send_pock));
int n = write(clientfd, buf, sizeof(buf));
if (n < 0)
{
cout<< "写入失败" << endl;
break;
}
}
}
/*
* chat_client_main.cpp
*
* Created on: 2012-4-19
* Author: root
*/
#include "chat_client.h"
int main(int argc,char** argv)
{
chat_client cc;
cc.NetInit();
cc.chat_info();
return 0;
}