- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对在尝试让 UTF-8 和瑞典语字符 ÅÄÖ 在 UNIX-talk 克隆中正确打印时遇到的问题感到困惑。
我已经使用 setLocale() 将语言环境设置为 sv_SE,并且我正在使用宽字符来尝试正确显示字符,小写的 åäö 工作正常,但不知何故大写变体不起作用。
下面是完整的代码,我怀疑我在 reader() sender() 或 putch() 中缺少字符大小。
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ncurses.h>
#include <pthread.h>
#include <signal.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sched.h>
#include <pthread.h>
#include <locale.h>
#include <wchar.h>
#define MATRIXSIZE 1000
#define STACKSIZE 10000
#define CHATLEN 2048
#include <syslog.h>
int r = 0, i = 0, mode = -1;
wchar_t mybuf[CHATLEN], tmbuf[CHATLEN];
WINDOW *me;
WINDOW *them;
struct stuff {
unsigned int col, row, size, realsize;
pid_t childpid;
pid_t mainpid;
char matrix[MATRIXSIZE];
char nukeline[1024];
int nukesize;
int terminate;
struct massaskit {
int writechan;
int readchan;
int sockfd;
struct sockaddr_in server;
struct sockaddr_in writeclient;
struct sockaddr_in readclient;
int c;
struct hostent *serverhost;
char hostname[256];
uint16_t port;
} bertil;
};
pthread_mutex_t scr_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_t sendthread; // Thread that listens to user's typing, and
// puts the characters on the screen, and
// transmits them over the network.
pthread_t readthread; // Thread that reads characters from the network
// and shows them on the screen.
int srv1(void *ptr)
{
struct massaskit *sockstuff = (struct massaskit *)ptr;
int opt = 1;
if ((sockstuff->sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket();");
exit(-1);
}
sockstuff->server.sin_family = AF_INET;
sockstuff->server.sin_addr.s_addr = INADDR_ANY;
sockstuff->server.sin_port = htons(sockstuff->port);
setsockopt(sockstuff->sockfd, SOCK_STREAM, SO_REUSEADDR, &opt,
sizeof(opt));
if (bind
(sockstuff->sockfd, (struct sockaddr *)&sockstuff->server,
sizeof(sockstuff->server)) < 0) {
perror("bind failed");
exit(-1);
}
if ((listen(sockstuff->sockfd, 3)) < 0)
{
perror("listen");
exit(-1);
}
sockstuff->c = sizeof(struct sockaddr_in);
printf("waiting for readchan on %i sockstuff->port..\n",
sockstuff->port);
sockstuff->readchan =
accept(sockstuff->sockfd,
(struct sockaddr *)&sockstuff->readclient,
(socklen_t *) & sockstuff->c);
printf("got a connection! now need a connection on writechan (p:%i)\n",
sockstuff->port);
sockstuff->writechan =
accept(sockstuff->sockfd,
(struct sockaddr *)&sockstuff->writeclient,
(socklen_t *) & sockstuff->c);
printf("got a connection! both read/write (p:%i)\n", sockstuff->port);
shutdown(sockstuff->writechan, SHUT_RD);
shutdown(sockstuff->readchan, SHUT_WR);
if (close(sockstuff->sockfd) != 0)
{
exit(1);
}
return 0;
}
int cli1(void *ptr)
{
/* srv1 starts with readchan, we start with writechan :-) */
struct massaskit *sockstuff = (struct massaskit *)ptr;
int opt = 1;
sockstuff->sockfd = -1; /* make it broken so other function understand */
if ((sockstuff->writechan = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("writechan->socket()");
exit(-1);
}
else
{
printf("socket init is ok.got fd[%i] [writechan]\n",
sockstuff->writechan);
}
setsockopt(sockstuff->writechan, SOCK_STREAM, SO_REUSEADDR, &opt,
sizeof(opt));
if ((sockstuff->serverhost =
gethostbyname(sockstuff->hostname)) == NULL) {
perror("error in resolving hostname :/\n");
exit(-1);
}
else
{
printf("ok, resolved host, now making connection [writechan]!\n");
}
memset(&sockstuff->server, '\0', sizeof(struct in_addr));
sockstuff->server.sin_family = AF_INET;
memcpy(&sockstuff->server.sin_addr.s_addr,
sockstuff->serverhost->h_addr,
(size_t) sockstuff->serverhost->h_length);
sockstuff->server.sin_port = htons(sockstuff->port);
if (connect
(sockstuff->writechan, (struct sockaddr *)&sockstuff->server,
sizeof(sockstuff->server)) == -1) {
perror("connection");
exit(-1);
}
else
{
printf("%s%s", "writechan established,starting readchan and", "sleeping 2s so other end can initalize the readchan.\n");
}
if ((sockstuff->readchan = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket();");
exit(-1);
}
else
{
printf("readchan socket init ok.\n");
fflush(stdout);
}
setsockopt(sockstuff->readchan, SOCK_STREAM, SO_REUSEADDR, &opt,
sizeof(opt));
if ((sockstuff->serverhost = gethostbyname(sockstuff->hostname)) == NULL)
{
perror("error in recieve channel, could'nt resolve host bailing.\n");
exit(-1);
}
else
{
printf("readchan could resolve host nice shit alabama\n");
fflush(stdout);
}
memset(&sockstuff->server, '\0', sizeof(struct in_addr));
sockstuff->server.sin_family = AF_INET;
memcpy(&sockstuff->server.sin_addr.s_addr,
sockstuff->serverhost->h_addr,
(size_t) sockstuff->serverhost->h_length);
sockstuff->server.sin_port = htons(sockstuff->port);
if (connect
(sockstuff->readchan, (struct sockaddr *)&sockstuff->server,
sizeof(sockstuff->server)) == -1) {
perror("connect()");
exit(-1);
}
else
{
printf("read chan estabiled. starting program!\n");
fflush(stdout);
}
return 0;
}
void putch(WINDOW * win, wchar_t ch)
{
syslog(LOG_INFO, "%04x", ch);
if (ch == 4 || ch == 7) // Translate left-arrow, backspace to CTL-H
ch = '\b';
if(ch < ' ' && ch != '\t' &&
ch != '\n' && ch != '\b'
)
{
return;
}
pthread_mutex_lock(&scr_mutex); // Get exclusive access to screen.
wechochar(win, ch);
if (ch == '\b')
{
wdelch(win);
refresh();
}
pthread_mutex_unlock(&scr_mutex);
}
void setupscreen()
{
int rows, cols;
initscr();
cbreak();
noecho();
intrflush(stdscr, FALSE);
rows = (LINES - 3) / 2;
cols = COLS - 2;
me = newwin(rows, cols, 1, 1);
them = newwin(rows, cols, rows + 2, 1);
idlok(me, TRUE);
scrollok(me, TRUE);
keypad(me, TRUE);
idlok(them, TRUE);
scrollok(them, TRUE);
border(0, 0, 0, 0, 0, 0, 0, 0);
move(rows + 1, 1);
hline(0, cols);
refresh();
}
void* sender(void *ptr) {
struct stuff *s = (struct stuff *)ptr;
setupscreen();
int ch;
while (1)
{
if (i > CHATLEN - 1)
{
i = 0;
}
ch = wgetch(me);
mybuf[i] = ch;
if (ch == KEY_RESIZE)
{
clear();
endwin();
setupscreen();
wchar_t *p = &mybuf[0];
while (&(*p) < &mybuf[CHATLEN - 1])
{
putch(me, (*p));
p++;
}
p = &tmbuf[0];
while (&(*p) < &tmbuf[CHATLEN - 1])
{
putch(them, (*p));
p++;
}
refresh();
}
else
{
putch(me, mybuf[i]);
int writefd = s->bertil.writechan;
write(writefd, &mybuf[i], sizeof(mybuf[i]));
}
i++;
}
pthread_cancel(sendthread);
return NULL;
}
void* reader(void *ptr) {
struct stuff *s = (struct stuff *)ptr;
int ch;
while(1)
{
if(r> CHATLEN - 1)
{
r = 0;
}
int readfd=s->bertil.readchan;
if((read(readfd,&ch,sizeof(ch))) == 0)
{
endwin();
refresh();
return 0;
}
tmbuf[r] = ch;
putch(them, tmbuf[r]);
r++;
}
pthread_cancel(readthread);
return NULL;
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "sv_SE");
memset(mybuf, 0, CHATLEN);
memset(tmbuf, 0, CHATLEN);
struct stuff s;
memset(&s, 0, sizeof(struct stuff));
if (argc == 1)
{
printf("usage %s port [host server on port]\n", argv[0]);
printf("usage %s port host [connecto host:port]\n", argv[0]);
exit(1);
}
else if (argc == 3)
{
s.bertil.port = (uint16_t) atoi(argv[1]);
memset(s.bertil.hostname, 0, 256);
memcpy(s.bertil.hostname, argv[2], strlen(argv[2]));
cli1(&s.bertil);
}
else if (argc == 2)
{
s.bertil.port = (uint16_t) atoi(argv[1]);
srv1(&s.bertil);
}
pthread_create(&readthread, NULL, reader, &s);
pthread_create(&sendthread, NULL, sender, &s);
pthread_join(sendthread, NULL);
pthread_join(readthread,NULL);
}
如果你想试试这个程序,编译它如下:
gcc file.c -lpthread -lncurses
服务:
./a.out 1234
连接:
./a.out 1234 localhost
如有任何帮助,我们将不胜感激!
最佳答案
实际上,wchar_t
已经足够了,符号扩展问题不太可能出现。
但是,在两个线程(sender
和reader
)中使用 ncurses 的应用程序,除非你专门编译它并允许互斥锁,否则它不会很好地工作. ncurses(就像 curses 的任何实现一样)使用全局变量来维护屏幕。多个线程将以意想不到的方式运行库。
进一步阅读:
5.7 (2 November 2008). This provides rudimentary support for threaded applications. It also distributes tack separately.
curs_threads
- curses thread support (ncurses 手册页)关于Capital ÅÄÖ 打印不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38311338/
我有以下内容: Creating new task(s) 我希望它显示为“创建新任务”。问题是,在 Chrome 中,它显示为“Creating New Task(S)”。带有大写字母 (S)。 显然
text:transform 的 css 属性有问题。当我将它设置为大写时,它在 IE7 和 IE8 中的行为不同。页面 [1] 的产品标题在其他浏览器中显示 apple tree 'Gala' 而在
我想知道以下情况的正则表达式: 该字符串在单行中包含一个大写单词,前面有两个换行符。在那之后,有几行字母数字字母(可能是非 ASCII utf-8)或者可能是空行。我想捕获一行中以大写单词开头并在下一
假设我有一个包含七百行文本的文件,全部都是大写字母。我应该如何将每行上的单词更改为小写,仅第一个字母保持大写? TEXT ON FIST LINE TEXT ON SECOND LINE TEX
我用错误的字符串更新了 Name_Table.column_2 数据。我更新了 “JohnSmith”,而不是“John Smith”。 现在我想替换多个字符串,例如:'JohnSmith' as '
我在这里解决了这个问题https://www.hackerrank.com/challenges/capitalize 描述:给你一个字符串。您的任务是将其中的每个单词大写。总之,只有第一个字符大写。
我对在尝试让 UTF-8 和瑞典语字符 ÅÄÖ 在 UNIX-talk 克隆中正确打印时遇到的问题感到困惑。 我已经使用 setLocale() 将语言环境设置为 sv_SE,并且我正在使用宽字符来尝
当查看 /usr/lib/ 中的库时,人们注意到几乎所有 lib*.so 文件都没有大写,即使官方库名称是,例如, BLAS: libblas.so, cURL: libcurl.so, BFD:li
我在 Python 中的某些字符串上使用了 capitalize 方法,其中一个字符串以空格开头: phrase = ' Lexical Semantics' phrase.capitalize()
题目地址:https://leetcode.com/problems/detect-capital/#/descriptionopen in new window 题目描述 Given a wor
如何告诉Vim的拼写检查器忽略首字母大写的单词? 令人讨厌的是,例如MyWidget被标记为拼写错误。 最佳答案 您可以定义语法元素以忽略拼写检查。 " Ignore CamelCase words
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我编写了一个函数来将字符串中的所有字母转为大写,但由于某种原因,当我在证据文件中使用它时,它给了我一个“总线错误”。谁能解释一下这是什么意思? 这是函数 void make_capital(char
我有一个 page that shows a javascript countdown . javascript 自动填充“d”表示天数,“h”表示小时数等...CSS 添加“ay(s)”、“our(
我有这个页面:http://cirkut.net/sub/avid/ 在我的 HTML 中,我的菜单项是大写的(主页、关于等),但我将它们文本转换为小写(我的品牌主要使用小写)。 我正在使用 sele
我创建了一个简单的程序来向体育迷询问一些信息。这是我到目前为止的代码: puts "What's your favorite pro sport?" favorite_sport = gets.cho
apache-commons-lang 库中有方便的方法StringUtils.capitalize()。但是我找不到它的成对逆方法,它将单词中的第一个字母变成小写。有这种方法吗? 最佳答案 您正在寻
我想通过大写月份的第一个字母来更改日期选择器。 目前我在线程中使用设置文化信息并在那里指定格式,但对于我的文化,月份总是全部小写: CultureInfo ci = new CultureInfo("
这个问题在这里已经有了答案: First lowercase the text then capitalize it. Is it possible with CSS? (4 个答案) 关闭 8 年
在 Ruby 中,我们有 'bang' 方法 capitalize! 如果没有对字符串进行任何更改,它会返回一个 nil 的奇怪行为。 这意味着我无法将此命令与其他命令链接在一起,因为如果它返回 ni
我是一名优秀的程序员,十分优秀!